Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

handling event in deep linked models #207

donnut opened this Issue Oct 25, 2012 · 2 comments


None yet
3 participants

donnut commented Oct 25, 2012

I have hierarchy of models with hasMany relations. Like the forum tutorial, but now a message can have multiple models instances as well, say paragraphs.

I'm having a hard time handling the add events. The first level view is

// thread view
this.model.on('change', this.render, this)
this.model.on('add:messages', this.render_message, this)

For the view on the deeper level I'm doing the same thing:

// message view
this.model.on('change', this.render, this)
this.model.on('add:paragraphs', this.render_paragraphs, this)

When fetch the complete hierarchy from the server, thus including threats, messages and paragraphs only, the thread change, thread add:message and message change events are triggered. The event add:paragraphs just doesn't seem to get triggered.

Any thoughts on why this could be?


Q: After a fetch, I don't get add: events for nested relations.
A: This is due to Backbone.Collection.reset silencing add events. Pass fetch( {add: true} ) to bypass this problem. You may want to override Backbone.Collection.fetch for this, and also trigger an event when the fetch has finished while you're at it. Example:

var _fetch = Backbone.Collection.prototype.fetch;
Backbone.Collection.prototype.fetch = function( options ) {
    options || ( options = {} );
    _.defaults( options, { add: true } );

    // Remove old models

    // Call 'fetch', and trigger an event when done.
    var dit = this,
        request = _fetch.call( this, options );
    request.done( function() {
            if ( !options.silent ) {
                dit.trigger( 'fetch', dit, options );

    return request;

DouweM commented Dec 20, 2012

@toddmorrison is right, this is expected behavior.

@DouweM DouweM closed this Dec 20, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment