0

我正在通过一个主干网络应用程序(对主干网络来说非常新)工作,并且在我获取它时需要一些帮助来迭代一个集合。我想要做的是循环遍历集合中的每个元素,如果progress属性100显示为导入<div>,则显示加载<div>

我有这个为集合中的最后一个元素工作,如下所示:

fileRepositoryCollection.fetch({

    success: function () {

        if (fileRepositoryCollection.at(fileRepositoryCollection.length - 1).get('progress') === 100) {
            $('#loading').hide();
            $('#imported').show();
        } else {
            $('#loading').show();
            $('#imported').hide();
        }
    }
});
4

1 回答 1

1

您不应该在集合类中呈现 html。你的集合视图应该有一个用于渲染的渲染方法,定义你的集合在 html 中的外观。然后,您在视图中遍历集合。

像这样的东西:

var fileRepositoryCollectionView = Backbone.View.extend({

    render: function() {
        // collection rendering

        this.collection.each(function(file) {
           if (file.get('progress') === 100) {
                $('#loading' + file.get('some_id')).hide();
                $('#imported' + file.get('some_id')).show();
            } else {
                $('#loading' + file.get('some_id')).show();
                $('#imported' + file.get('some_id')).hide();
            }
        });

        return this;
    },
});

var repView = new fileRepositoryCollectionView({ collection: fileRepositoryCollection});
于 2013-04-27T16:11:00.873 回答