varvet / serenade.js

Client side MVC framework
http://serenadejs.org
Other
524 stars 27 forks source link

Attributes changes in children aren't automatically persisted #61

Closed mptre closed 12 years ago

mptre commented 12 years ago

This bug first occurred with v0.1.1. Unfortunately it seems to back with v0.2.1. In order to reproduce this bug follow the instructions below:

mptre commented 12 years ago

Please let me know if I can supply you with any more info or examples.

jnicklas commented 12 years ago

What's happening is that the persisted model is Application, and it has children which are Post instances. When one of the Post instances changes it does not cause the Application model to persist itself.

Once we support inverse associations (see #8), we might be able to make this work more magically, by checking whether the association is serialized or not.

Until then, you're going to have to manually persist the application model after you change the post attribute. You can do this by calling my_application.save(). You should also be able to add this to your model layer, so you don't have to explicitly do it in the controller.

mptre commented 12 years ago

Thanks for the info. Is the change event triggered recursively on the models? Can I simply bind to the change event on the Application model and then call the save method?

Application.bind('change', function () {
    this.save();
});
jnicklas commented 12 years ago

No, you can't. That's the whole problem. If it were called "recursively" you wouldn't have to do anything.

mptre commented 12 years ago

Ah sorry, I'm the one being a bit slow here. Found a solution that works by now.


var application = Application.find(1);
var Post = Serenade.Model.extend('post', function () {
    this.bind('change', function () {
        application.save();
    });
});
jnicklas commented 12 years ago

Yes that should do it! Ideally we'll be able to infer this automatically in the future.