Backbone : listen to add/remove event of a collection inside model in a collection.

Heisenberg

I have a collection x (one instance) of models y.

var y = Backbone.Model.extend({
        defaults: {
            name: null,
            otherCollection: null
        }
    });

otherCollection (multiple instances) is a collection of models z.

Which event do I listen to inside collection x so that I would get notified when any z is added in / removed from otherCollection?

Peter Wagener

Backbone by default only support event propagation from Contained Model --> Containing Collection. It won't do the sort of deep nested event propagation you're looking for without some extra effort.

To do it from Contained Collection --> Containing Model, you could do something like the following. Here we're setting up an EpisodeCollection, which will be contained in a SeriesModel. Whenever changes happen to the episode collection changes, we want to hear about it in the series:

var EpisodeCollection = Backbone.Collection.extend({
    comparator: 'id'
    // ... whatever you want
});

var SeriesModel = Backbone.Model.extend({
    initialize: function() {
        if (this.has('episodes')) {
            this.listenTo(this.get('episodes'), 'all', this._onEpisodeChange);
        }
    },

    _onEpisodeChange: function() {
        var eventArgs = Array.prototype.slice.call(arguments, 1);
        var eventName = arguments[0];

        var toTrigger = 'change:episodes:' + eventName + ' change:episodes change';
        this.trigger(toTrigger, eventArgs);
    }    
});

Here's a running JSFiddle illustrating this with a glorious Star Wars example:

http://jsfiddle.net/9kn6uqdn/4/

You can imagine building similar connections for deeper nesting as needed.

A great thing about Backbone is that it isn't an opinionated framework; it leaves the opinions to the application implementors (us!). To that end, I have tried deeply nested event propagation on a few different occasions, and each time have ended up going an alternative direction. Even in the simple example above, you can see that modifying the title of Episode 6 actually fires 6 different events (which likely isn't what you expect).

You can improve this with more effort, but it can get startlingly complex very quickly. Event propagation in large apps can be hard to reason about, so in my mind the simpler the event flow the easier it is to manage & maintain.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Collection inside a Backbone model

From Dev

Backbone populate Model inside of a Collection

From Dev

Backbone listen to collection, find out which model has changed

From Dev

How to trace an event trigger in backbone (add triggered for every model in collection)

From Dev

Model collection inside a Model

From Dev

Model collection inside a Model

From Dev

Backbone Collection 'add' event not firing

From Dev

Backbone binding to add event on collection

From Dev

Backbone binding to add event on collection

From Dev

Backbone Marionette collection on DOM event

From Dev

Backbone collection add event not firing

From Dev

Backbone Collection not using it's model

From Dev

Backbone treat model like collection

From Dev

Backbone-Event: add array of models to collection

From Dev

Backbone-Event: add array of models to collection

From Dev

backbone click event not working in collection view

From Dev

Backbone - aggregator event firing for all models in collection

From Dev

Backbone - remove last model in Collection, but only by filter

From Dev

Set a Backbone collection model with circular dependencies in requirejs

From Dev

BackBone collection takes array of models as a single model?

From Dev

Undefined model prototype in Backbone Collection and Marionette CompositeView

From Dev

Backbone JS populating model/collection correctly

From Dev

Backbone: collection adds random model to itself

From Dev

Backbone Collection get Model by it's ID

From Dev

Move up/down a model in a backbone collection

From Dev

Backbone collection not updated with model from REST response?

From Dev

Adding model to a collection after save method in backbone

From Dev

Backbone Model and Collection url design with a NodeJS server

From Dev

Backbone - remove last model in Collection, but only by filter

Related Related

  1. 1

    Collection inside a Backbone model

  2. 2

    Backbone populate Model inside of a Collection

  3. 3

    Backbone listen to collection, find out which model has changed

  4. 4

    How to trace an event trigger in backbone (add triggered for every model in collection)

  5. 5

    Model collection inside a Model

  6. 6

    Model collection inside a Model

  7. 7

    Backbone Collection 'add' event not firing

  8. 8

    Backbone binding to add event on collection

  9. 9

    Backbone binding to add event on collection

  10. 10

    Backbone Marionette collection on DOM event

  11. 11

    Backbone collection add event not firing

  12. 12

    Backbone Collection not using it's model

  13. 13

    Backbone treat model like collection

  14. 14

    Backbone-Event: add array of models to collection

  15. 15

    Backbone-Event: add array of models to collection

  16. 16

    backbone click event not working in collection view

  17. 17

    Backbone - aggregator event firing for all models in collection

  18. 18

    Backbone - remove last model in Collection, but only by filter

  19. 19

    Set a Backbone collection model with circular dependencies in requirejs

  20. 20

    BackBone collection takes array of models as a single model?

  21. 21

    Undefined model prototype in Backbone Collection and Marionette CompositeView

  22. 22

    Backbone JS populating model/collection correctly

  23. 23

    Backbone: collection adds random model to itself

  24. 24

    Backbone Collection get Model by it's ID

  25. 25

    Move up/down a model in a backbone collection

  26. 26

    Backbone collection not updated with model from REST response?

  27. 27

    Adding model to a collection after save method in backbone

  28. 28

    Backbone Model and Collection url design with a NodeJS server

  29. 29

    Backbone - remove last model in Collection, but only by filter

HotTag

Archive