Wolfgang Adamec
Wolfgang Adamec

Reputation: 8524

Backbone.js event system (model and collection)

In Backbone.js (0.9.2) I have the following:

One collection and one method that gets called when something changes

this.collection.on("change", this.methodOne, this);

When I change a value of one model of this collection ...

model.set("value1", "abc");

... the change event from the model BUBBLES UP to the collection and so "methodOne" is called.

When I look into the source code of Backbone.js, "collection", method "add", a private method "_prepareModel" is called that makes the model - collection reference:

model.collection = this;

But from there on I do not understand how and in which methods this bubble up mechanism is functioning?

Has anybody got information for me about that?

Thanks alot in advance!

Wolfgang

Upvotes: 1

Views: 975

Answers (1)

rinat.io
rinat.io

Reputation: 3188

Take a look at Backbone.Collection _onModelEvent method. In the docs:

Internal method called every time a model in the set fires an event. Sets need to update their indexes when models change ids. All other events simply proxy through. "add" and "remove" events that originate in other collections are ignored.

Upvotes: 2

Related Questions