1

我将尝试详细解释我的问题,但如果缺少某些内容,请戳我,我很乐意为您提供更多信息..

我正在使用主干 0.9.10,主干关系 0.7.1,下划线 1.4.4。

我已经设置了一个这样的骨干关系集合:

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

..并创建了一个主干视图,它将侦听器动态附加到传递的集合:

// 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');
    }
});

现在,好的部分来了:

我使用关系的 .findOrCreate 方法来创建一个新的 ActorAssignment 模型:

// 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);
}    

以前,在主干 0.9.2 中,'sort' 事件总是在 'add' 事件之后触发,但现在不再触发。我花了几个小时试图调试这个,这是我的结果。似乎模型上的“添加”传播到集合被延迟,而“排序”直接在集合上触发,这导致我的应用程序出现奇怪的行为.

我在主干源代码中放了一些调试语句,这是输出:

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

如您所见,onAdd 是在 onCollectionSort 之后触发的,而 'sort' 事件是在骨干 collection.add 中的 'add' 事件之后触发的,在第 644 行附近。上面是有意的吗?或者它是一个错误?另外,我不知道这是一个特定的关系问题,还是 Backbone 本身有问题。最后,我的问题是:事件应该像上面的例子那样是非阻塞的,还是我们应该更加谨慎地假设事件是按特定顺序触发的?

编辑:我将 process_model 属性添加到 findOrCreate 调用中,因为这导致了我的问题,答案如下。

4

1 回答 1

1

我找到了!显然,在与另一个模型有关系的集合模型上调用 findOrCreate 会导致主干.relational 自动解析关系并将其添加到 processModel 上我的 oneToMany 的逆关系集合中,这会在我实际添加模型之前触发添加到收藏。希望这对遇到此问题的其他人有意义。

于 2013-03-12T09:11:26.177 回答