Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching rv-if causes binder to set up multiple times #678

Open
Namek opened this issue Sep 5, 2016 · 3 comments
Open

Switching rv-if causes binder to set up multiple times #678

Namek opened this issue Sep 5, 2016 · 3 comments

Comments

@Namek
Copy link
Contributor

Namek commented Sep 5, 2016

rv-if going to falsy doesn't destroy elements, it seems to just remove them from DOM and keep in memory.

STR:

  1. set rv-if to truthy, binder's function should be called
  2. set rv-if to falsy, nested elements should be removed from DOM
  3. set rv-if to truthy again, binder function is called on same element as previously

When rv-if turns to truthy then it readds nested elements. However, binder (checked only one-way binders) setup function is being called as many times as rv-if is switched to truthy state.

Fiddle

Here's the fiddle presenting the problem: https://jsfiddle.net/qzyLby25/1/

Spam "toggle state" button and observe new elements appearing which is inappropriate.

It does not matter if rv-if was truthy or falsy from the beginning which can be seen here: https://jsfiddle.net/gzyL2wnd/

My commentary on this:

in AngularJS there's ng-show (hiding elements using CSS) and ng-if (removing elements). Here rv-if seems to be something inbetween, because it removes elements but readds them instead of recreating.

Although it's pretty interesting solution it seems to be inconsistent with binders, am I wrong?

@jccazeaux
Copy link
Contributor

That's an interesting case. I do not have a direct answer, but as I see it you should implement unbind function on your binder to correctly handle the unbinding.

@blikblum
Copy link
Contributor

I did a test and removed nested.bind / unbind calls inside if.routine except the bind after nested is created.
The tests passes and the behavior of this issue is not seem.

I think the reason to unbind / bind inside if routine is an optimization to avoid the nested view to be updated when its model data changes. Although is questionable since the cost to unbind/rebind may be bigger than the eventual observers calls in the nested view while it is hidden

@jccazeaux
Copy link
Contributor

After some time i think the optimization causes some problems. If a binder is unbound it should be totally removed and not "cached" for further use. The problem can be seen in components too. See this fiddle

https://jsfiddle.net/gzyL2wnd/1/

We see that a unbound component keeps a state. It's not logic. The initialize function should be called any time the component is bound. If we want to keep a state we don't use rv-if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants