-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Dynamically displayed component being rendered more than once #2163
Conversation
Can you put together an example in jsfiddle? |
Sure. I'll do it this afternoon after work. |
@mbest I am trying to wrap this up this morning, but it revolves around loading the component using something like require. I'm having a difficult time getting it to work in JsFiddle. Would something you can clone from github work? |
@codymullins Yeah I'll give that a shot. |
@mbest I might have just found out where this is coming from. I turned off deferred updates and the problem is fixed. I think I read somewhere it was expected with deferred updates that a component could be rendered twice? |
I don't know why it would be expected. Perhaps this is related to #2174. Could you try applying that patch and see if it makes a difference? |
I pulled down that branch and built it, doesn't appear to make a difference. If I call |
Thanks for checking that out and the additional information. Can you share the code that sets the observable? |
hey @mbest I don't know why I couldn't replicate it before, but here's what I'm experiencing: https://jsfiddle.net/11umxthr/ If you open up the console before you click the button, you'll see the console.log statement gets executed twice. if you uncomment |
Why are you setting |
In this scenario there's some logic to determine which component to display based on what's in the route. I think previously I was using an if: binding and setting the currentFileComponent to null at least until one was chosen. (the example doesn't include that, I whittled it down to bare bones). |
Your example exposes an issue that when using deferred updates a subscriber can get a notification that occurred before it subscribed to the observable. When you change the observable's value, the notification is scheduled. Afterwards the component binds using the latest value. But then it gets the notification of the same value. As you noted, a workaround is to call A possible fix for this issue is for Knockout to only call subscribers that existed when the notification was scheduled. |
…notify those present and not future subscriptions.
I've attached the fix here. |
Awesome. That was quick, you rock. I will pull this down and test it out. |
I can confirm this fixes the issue. I don't see any other evident side effects either. |
…o only notify those present and not future subscriptions.
@mbest since this is merged in, are you planning the 3.4.2 release anytime soon? |
I only just found this related SO post: http://stackoverflow.com/questions/36281676/knockout-3-4-0-deferupdates-and-reevaluation-count |
Hopefully. I just added another issue to consider for 3.4.2, plus there's a possible regression in 3.4.1 that I want to look into. |
@codymullins 3.4.2 is now released. |
codymullins:
I've been battling this one for a few weeks, and just now got confirmation that someone else was able to reproduce this and I wasn't just going crazy.
The issue arises when I am dynamically displaying a component based on some logic - in this case, I'm parsing the route and grabbing a parameter. When the component is looking at an observable to determine the name, it's being loaded twice in the
getComponent
call and thus my constructor is called twice. If I bind the same form with a literal string, the component only renders once.Here's an example (it has the same issue even without the outer
if
binding):