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

Live Binding Broken with Models with Nested Sub-models #457

Closed
thecountofzero opened this Issue Aug 9, 2013 · 10 comments

Comments

Projects
None yet
2 participants
@thecountofzero
Contributor

thecountofzero commented Aug 9, 2013

This is occurs with models that have nested models as attributes.

For example, a player model has an attribute named team which is a model. The team model has an attribute named players which is a list of player models which contains the original player model. So A contains B and B contains A.

This is all setup by using the attribute plugin.

I issue a findOne for the player model and pass the player to an EJS for rendering (with live binding).

The problem is that if later I do another findOne for the same model and any of its live binded value have changed, they are not being updated in the UI.

After a few hours of WTFs and digging, here's what I discovered.

When the initial findOne is executed and Can is parsing the incoming JSON into the appropriate models, here's what seems to happen.

It creates a new model for the player model and then begins parsing all of it's attributes. When it gets to the "team" attribute it knows from the attributes property of the player model that "team" is a team model, so it creates a new team model which it then begins to process. When it gets to the "players" attribute it says "ok these are player models" and creates new player models. It even creates another new player model for the player data that matches the original player model that all of this data is a part of. They have different _cid values.

When the processing/parsing is complete, findOne is resolved with the outermost instance of the player model. That is the instance that is passed into the EJS. However that is not the instance that is in the model store. The instance in the model store is the one that is in the "player" attribute of the team model. Therefore live binding is broken.

Please see the attached fiddle. When you click the "Fetch Game" button, I am changing the fixture to point to a different data set. This should result in the value printed for league to be changed from "League of Kings" to "Hoboken Heroes", but it doesn't.

If, however, you comment out line #14 which sets up the association between a team and its games attribute, then live binding works.

http://jsfiddle.net/thecountofzero/HMYCX/

@justinbmeyer

This comment has been minimized.

Show comment
Hide comment
@justinbmeyer

justinbmeyer Aug 9, 2013

Contributor

This isn't a problem with live binding it seems, more the model store. If you change the appropriate instance, does the page update?

It's probably pretty easy to submit a breaking test for this. Re-create the model association and do new Player(data) with the appropriate data.

Then go through the instances it created and verify that ones sharing the same id share the same cid.

Contributor

justinbmeyer commented Aug 9, 2013

This isn't a problem with live binding it seems, more the model store. If you change the appropriate instance, does the page update?

It's probably pretty easy to submit a breaking test for this. Re-create the model association and do new Player(data) with the appropriate data.

Then go through the instances it created and verify that ones sharing the same id share the same cid.

@thecountofzero

This comment has been minimized.

Show comment
Hide comment
@thecountofzero

thecountofzero Aug 9, 2013

Contributor

You are correct. It is not a problem with live binding as the page does update if you update the appropriate instance.

I will create the breaking test for this tomorrow.

Thanks for the reply.

Contributor

thecountofzero commented Aug 9, 2013

You are correct. It is not a problem with live binding as the page does update if you update the appropriate instance.

I will create the breaking test for this tomorrow.

Thanks for the reply.

@thecountofzero

This comment has been minimized.

Show comment
Hide comment
@thecountofzero

thecountofzero Aug 16, 2013

Contributor

Was just about to submit a failing test, but it looks like someone beat me to it.

Contributor

thecountofzero commented Aug 16, 2013

Was just about to submit a failing test, but it looks like someone beat me to it.

@thecountofzero

This comment has been minimized.

Show comment
Hide comment
@thecountofzero

thecountofzero Aug 16, 2013

Contributor

Test I see in model_test.js is for Issue #357

Are these issues related?

Contributor

thecountofzero commented Aug 16, 2013

Test I see in model_test.js is for Issue #357

Are these issues related?

thecountofzero added a commit to thecountofzero/canjs that referenced this issue Aug 16, 2013

@thecountofzero

This comment has been minimized.

Show comment
Hide comment
@thecountofzero

thecountofzero Sep 14, 2013

Contributor

@justinbmeyer, hey dude, any time to look at this one?

Contributor

thecountofzero commented Sep 14, 2013

@justinbmeyer, hey dude, any time to look at this one?

@thecountofzero

This comment has been minimized.

Show comment
Hide comment
@thecountofzero

thecountofzero Sep 20, 2013

Contributor

Somehow we need to keep track of models of the same model type and ID while they are being processed in "model"

Contributor

thecountofzero commented Sep 20, 2013

Somehow we need to keep track of models of the same model type and ID while they are being processed in "model"

@ghost ghost assigned justinbmeyer Sep 22, 2013

@justinbmeyer

This comment has been minimized.

Show comment
Hide comment
@justinbmeyer

justinbmeyer Sep 22, 2013

Contributor

I'll take a look at it now ...

Contributor

justinbmeyer commented Sep 22, 2013

I'll take a look at it now ...

@justinbmeyer

This comment has been minimized.

Show comment
Hide comment
@justinbmeyer

justinbmeyer Sep 22, 2013

Contributor

Thanks for the test, but don't create globals in them :-).

Game = can.Model({
Contributor

justinbmeyer commented Sep 22, 2013

Thanks for the test, but don't create globals in them :-).

Game = can.Model({
@thecountofzero

This comment has been minimized.

Show comment
Hide comment
@thecountofzero

thecountofzero Sep 22, 2013

Contributor

Whoops. Forgot "var". Bad me.

Contributor

thecountofzero commented Sep 22, 2013

Whoops. Forgot "var". Bad me.

@justinbmeyer

This comment has been minimized.

Show comment
Hide comment
@justinbmeyer

justinbmeyer Sep 22, 2013

Contributor

I fixed this by adding things to the store before the attributes are processed.

Contributor

justinbmeyer commented Sep 22, 2013

I fixed this by adding things to the store before the attributes are processed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment