dhruvaray / backbone-associations

Create object hierarchies with Backbone models; Respond to hierarchy changes using regular Backbone events.
http://dhruvaray.github.io/backbone-associations/
MIT License
492 stars 75 forks source link

Question: Best way to listen for changes on deep nested objects. #155

Open swhamilton opened 9 years ago

swhamilton commented 9 years ago

I need similar functionality to what Backbone-nested provides. I need to listen to an event change on a deep nested object, but I don't really need the extra features that backbone-associated offers.

data from server:

{
  name: "test",
  photo: {
    profile: {
      prefix: "http://",
      suffix:"profile.jpg"
    },
    team: {
      prefix: "http://",
      suffix:"team.jpg"
    }
  }
}

I would listen to the change event on the photo.profile and photo.team object, which are simple models:

var Photo = Backbone.Model.extend({
  default: {
    prefix: "",
    suffix: ""
  }
});

Is there a simple way to achieve this with backbone-associations? Or I should use both BB-associations in conjunction with BB-nested or BB-deep-model?

jameslai commented 9 years ago

Yes, you can listen for nested-change events on the parent model as long as you set Backbone.Associations.EVENTS_NC to true. There is a bit of documentation for this on the [events documentation](Backbone.Associations.EVENTS_NC = true) page.