Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Live Binding Broken with Models with Nested Sub-models #457
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.
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.