繁体   English   中英

木偶事件,从子事件到父事件的异步回调?

[英]Marionette Events, async callback from child to parent event?

我有一个索引视图,它调用了集合,因此也调用了项目视图。 用户可以在项目上打勾,然后单击“在索引模板中复制”。

下面的代码工作正常,该项目在索引中触发了一个事件。 但是,问题是,我希望能够知道所有事件何时都已复制完这些帖子。 我能想到的唯一方法是将复制逻辑移入索引视图,但我确实将此视为项目视图操作。

文章/ index.js

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

我认为您应该对此进行重新设计以保存一组模型,而不是每次保存都会触发一个事件。 这样,您可以在使用Jquerys $ .when在完成所有操作后触发回调时听一组XHRS。

请参阅Brian Mann的解决方案,他允许任何数量的可同步主干实体触发"when:fetched"事件,但是可以使用相同的方法来保存模型。 好的视频

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM