关于backbone collection的疑问?

var ListView = Backbone.View.extend({
            initialize: function() {
                if(this.collection) {
                    this.byId = {};
                    this.views = [];
                    this.collection.each(this.registerView,this);
                }
            },
            registerView: function(model) {
                var view = new ItemView({model: model});
                this.byId[model.cid] = view;
                this.views.push(view);
            },
            render: function() {
                var self = this;
                this.$el.empty();
                _.each(this.views, function(view) {
                    $_el = view.render().$el;
                    self.$el.append($_el);
                });
            }
        });
        
        var aView = new ListView({el: "#alist", collection: alist});

        aView.render();

代码如上:
图片描述

this.collection.each方法第二个参数传this,代表什么意思?
第一个参数直接调用registerView方法,方法里没有传model,那model是从哪里来的呢?

阅读 2.5k
1 个回答

你找找文档吧。

this.collection.each方法第二个参数传this,代表什么意思?
答:我猜这个应该是一个绑定上下文的。

registerView方法,方法里没有传model。
答:就和jquery的each一样。里面他是会传参数的。比如这样$(selector).each(function(index,element))

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题