73

backbone.js – 在获取集合或模型时触发加载视图

 5 years ago
source link: https://codeday.me/bug/20190112/505264.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

我一直在使用木偶一周,这让我的生活变得更轻松!

现在我需要能够在获取集合或模型时通知用户,因为某些视图需要花费大量时间来呈现/获取.举个例子,我做了一个小模型:

当用户单击某个类别时,需要加载该类别中所有项目的集合.在获取集合之前,我想显示加载视图,如图所示(视图1).实现这一目标的优雅解决方案是什么.我发现以下帖子中用户启用了获取触发器: http://tbranyen.com/post/how-to-indicate-backbone-fetch-progress .这似乎有效但不是我想要的.这是我提出的:

var ItemThumbCollectionView = Backbone.Marionette.CollectionView.extend({
        collection: new ItemsCollection(userId),
        itemView: ItemThumbView,
        initialize: function(){
            this.collection.on("fetch", function() {
                //show loading view
            }, this);
            this.collection.on("reset", function() {
                //show final view
            }, this);
            this.collection.fetch();

            Backbone.history.navigate('user/'+identifier);
            this.bindTo(this.collection, "reset", this.render, this)
        }
    });

如果我可以有一个名为’LoadItemView’的可选属性,那就太好了.这将在获取期间覆盖itemView.在您看来这是一个很好的做法吗?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK