Javascript 木偶事件,从子事件到父事件的异步回调?
我有一个索引视图,它调用集合,从而调用项视图。用户可以勾选项目,然后单击“在索引模板中复制” 下面的代码工作正常,该项在索引中触发了一个事件。然而,问题是,我想知道所有事件何时完成了对这些帖子的复制。我能想到的唯一方法是将复制逻辑移动到索引视图中,但我确实认为这是一个项目视图操作 posts/index.jsJavascript 木偶事件,从子事件到父事件的异步回调?,javascript,events,asynchronous,marionette,Javascript,Events,Asynchronous,Marionette,我有一个索引视图,它调用集合,从而调用项视图。用户可以勾选项目,然后单击“在索引模板中复制” 下面的代码工作正常,该项在索引中触发了一个事件。然而,问题是,我想知道所有事件何时完成了对这些帖子的复制。我能想到的唯一方法是将复制逻辑移动到索引视图中,但我确实认为这是一个项目视图操作 posts/index.js // copy posts from the collection onClickCopyPosts: function() { // are we
// copy posts from the collection
onClickCopyPosts: function() {
// are we sure we want to delete these posts
var platform = $('#copyPlatforms option:selected').val();
if (confirm('Are you sure you want to copy these posts to that platform?')) {
// get the post ids of the ticked posts
var postIds = this.getSelectedPostsArray();
_.each (postIds, function (postId, key) {
MyApp.vent.trigger('post:copy:'+postId, platform);
})
}
},
item.js
initialize: function () {
var that = this;
this.listenTo(MyApp.vent, 'post:copy:'+this.model.get('id'), function(platform) {
// fetch the collection and pass its filters through
that.onCopy(platform);
});
},
onCopy: function(platform) {
// copy the current model to a new variable
var modelJSON = this.model.toJSON();
// set platform to the platform passed to the method
modelJSON.platform = platform;
modelJSON = Factory.create(modelJSON);
// create a new instance of that model
var newModel = new Model(modelJSON);
// save the new model
newModel.save({}, {
success: function() {
//console.log('copy post', 'success');
},
error: function() {
//console.log('copy post', 'error');
},
complete: function() {
//console.log('copy post', 'complete');
}
});
return;
},
我认为您应该重新设计它来保存一组模型,而不是每次保存都触发一个事件。通过这种方式,您可以听到一组XHR使用Jquerys在完成所有操作后发出回调 请参阅,he允许任意数量的可同步主干实体在:获取时触发
“
事件,但可用于保存模型。很好