I'll try to explain my problem in detail, but if something's missing, please poke me and I'll gladly give you additional info..

I'm using backbone 0.9.10, backbone-relational 0.7.1, underscore 1.4.4.

I've setup a backbone relational collection like this:

// ActorAssignmentCollection.js
Backbone.Collection.extend({
    model: AbstractActorAssignment,
    comparator: function (assignment) {
        return assignment.get('sort_index');
    }
});

..and created a backbone view which dynamically attaches listeners to a passed collection:

// ActorDisplay.js
Backbone.View.extend({

    attachListeners: function (collection) {
        this.listenTo(collection, 'add', _.bind(this.onAdd, this));
        this.listenTo(collection, 'sort', _.bind(this.onCollectionSort, this));
    },

    onAdd: function () {
        console.log('onAdd fired');
    },

    onCollectionSort: function() {
        console.log('onCollectionSortFired');
    }
});

Now, here comes the good part:

I use relational's .findOrCreate method for creating a new ActorAssignment model:

// AddAssignmentCommand:
execute: function (data) {
    var sortIndex = data.collection.length,
        actorAssignment = data.collection.model.findOrCreate({
            actor: data.actor,
            sort_index: sortIndex,
            process_model: data.processModel
        });

    data.collection.add(actorAssignment);
}    

Before, with backbone 0.9.2, the 'sort' event was always fired after the 'add' event, but not anymore. I've spent hours trying to debug this, and here is my outcome.. It seems the propagation of 'add' on model to collection is delayed, while 'sort' is directly triggered on the collection, which causes strange behaviour in my application.

I've put some debugging statements in the backbone source code, this is the output:

triggering add events.. 6dd47c9_backbone_1.js:641
triggering add event on model 6dd47c9_backbone_1.js:644
trigger: add fired 6dd47c9_backbone_1.js:182
triggering sort events.. 6dd47c9_backbone_1.js:647
trigger: sort fired 6dd47c9_backbone_1.js:182
*onCollectionSortFired* ControlStructureActorDisplay.js:136
trigger: relational:add fired 6dd47c9_backbone_1.js:182
trigger: change:entity_type fired 6dd47c9_backbone_1.js:182
trigger: change:entity_type fired 6dd47c9_backbone_1.js:182
trigger: change:entity_type fired 6dd47c9_backbone_1.js:182
trigger: change:entity_type fired 6dd47c9_backbone_1.js:182
trigger: change:actor fired 6dd47c9_backbone_1.js:182
trigger: change:actor fired 6dd47c9_backbone_1.js:182
trigger: change:actor fired 6dd47c9_backbone_1.js:182
trigger: change:actor fired 6dd47c9_backbone_1.js:182
trigger: change:process_step fired 6dd47c9_backbone_1.js:182
trigger: change:process_step fired 6dd47c9_backbone_1.js:182
trigger: change:process_step fired 6dd47c9_backbone_1.js:182
trigger: change:process_step fired 6dd47c9_backbone_1.js:182
trigger: change:sort_index fired 6dd47c9_backbone_1.js:182
trigger: change:sort_index fired 6dd47c9_backbone_1.js:182
trigger: change:sort_index fired 6dd47c9_backbone_1.js:182
trigger: change:sort_index fired 6dd47c9_backbone_1.js:182
trigger: change fired 6dd47c9_backbone_1.js:182
trigger: change fired 6dd47c9_backbone_1.js:182
trigger: change fired 6dd47c9_backbone_1.js:182
trigger: change fired 6dd47c9_backbone_1.js:182
trigger: add fired 6dd47c9_backbone_1.js:182
trigger: add fired 6dd47c9_backbone_1.js:182
trigger: add:actor_assignments fired 6dd47c9_backbone_1.js:182
trigger: add:actor_assignments fired 6dd47c9_backbone_1.js:182
trigger: add fired 6dd47c9_backbone_1.js:182
*onAdd fired* ControlStructureActorDisplay.js:155

As you can see, the onAdd is fired way after the onCollectionSort, while the 'sort' event is triggered AFTER the 'add' event in backbone collection.add around line 644. Is above intended? Or is it a bug? Also, I don't know if this is a specific Relational problem, or something wrong with Backbone itself. Finally, my question raises: should events be non blocking as in above example, or should we be more cautious with assuming events are fired in a specific order?

EDIT: I added the process_model property to the findOrCreate call, because that caused my issue, the answer is below.

有帮助吗?

解决方案

I found it! Apparently, calling findOrCreate on a collection model, with a relation to another model, causes backbone.relational to resolve the relation automatically and adds it to the inverse relation collection of my oneToMany on the processModel, which triggers an add before I actually add my model to the collection. Hope this make sense for others who run into this problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top