16

我有一个问题,我认为是非常基本的东西,但是:

我只看到了集合视图和依赖于正在更新的集合的单个视图的示例。如果您有多个视图试图订阅一个集合事件,例如 reset、addOne、addAll 等,该怎么办?

我是否错过了关于做/不做这件事的一些要点?你有这方面的例子吗?这甚至有意义吗?

任何信息都非常感谢

    var Coll = Backbone.Collection.extend({
        model: SingleModel,
        url: 'service',
        initialize: function(){
            console.log('collection inited')

        }        

    });

    var SingleModel = Backbone.Collection.extend({});            

    var CollView = Backbone.View.extend({                
        el: 'ul',
        template: Handlebars.compile(someContainerTemplate),
        init: function(){
            _.bindAll(this, 'render', 'addAll', 'addOne');
            this.collection.bind("reset", this.addAll);

            this.collection.fetch();
        },
        render: function(){
            $(this.el).append(this.template())
        },

        addAll: function(){
            this.collection.each(this.addOne);
        },

        addOne: function(model){
            var view = new SingleView({ model: model })            
        }
    })

    var SingleView = Backbone.View.extend({
        tagName: "li",
        events: {
            "click .delete": "remove"                    
        },
        template: Handlebars.compile(someTemplateForSingleItem),  
        initialize: function() {
        _.bindAll(this,'render');

            this.model.bind('save', this.addOne);
            this.model.bind('destroy', removeEl);
        },

        remove: function(){
          this.model.destroy();  
        },

        removeEl: function(){
          $(this.el).remove();  
        },

        render: function() {
            var context = this.model.toJSON();
            return $(this.el).append(this.template(context));
        },       
    })


    // standard so far (excluding any bad practices), 
    // but what if you have another view dependent on 
    // say the number of length of the collection, and 
    // you want it to update if any single models are destroyed

    var HeaderView = Backbone.View.extend({
        tagName: "div#header",
        template: Handlebars.compile(someHeaderTemplate), 
        initialize: function() {
        _.bindAll(this,'render');

        this.model.bind('save', this.addOne);
        },

        render: function() {
            //assigning this collection length

            var context = this.collection.length;
            return $(this.el).append(this.template(context));
        },      
    });        

    var coll = new Coll();
    new CollView({ collection: coll });
    new HeaderView({ collection: coll});
4

2 回答 2

30

您所做的一切都很好,这也是使用 Backbone 的部分原因。从骨干介绍

每当一个 UI 动作导致模型的某个属性发生变化时,模型就会触发一个“变化”事件;所有显示模型状态的视图都可以收到更改通知,

请注意,他们说的是“所有视图”,而不是“视图”。

单个集合的多个视图的一个示例是聊天系统。假设您有一组在线用户;那么您可能在标题中有一个显示在线人数的简单视图和列出用户的另一个视图(同一集合):

var User = Backbone.Model.extend({});
var OnlineUsers = Backbone.Collection.extend({
    model: User
});

var CounterView = Backbone.View.extend({
    tagName: 'span',
    initialize: function() {
        _.bindAll(this, 'render');
        this.collection.on('add', this.render);
        // other interesting events...
    },
    render: function() {
        this.$el.text(this.collection.size());
        return this;
    }
});
var ListView = Backbone.View.extend({
    initialize: function() {
        _.bindAll(this, 'render');
        this.collection.on('add', this.render);
        // other interesting events...
    },
    render: function() {
        var html = this.collection.map(function(m) {
            return '<p>' + m.get('name') + '</p>';
        });
        this.$el.html(html.join(''));
        return this;
    }
});

然后您将拥有一个OnlineUsers实例,但您的实例CounterViewListView实例都在观看它。当人们上线或下线时,两种视图都将根据需要进行更新。

简单演示:http: //jsfiddle.net/ambiguous/eX7gZ/

上面的示例情况听起来正是您正在做的事情,而这正是 Backbone 的用途。做得好。

于 2012-04-26T21:45:27.610 回答
2

我们遇到了同样的问题。我们需要为每个模型使用具有唯一内部事件的多个 jQuery 模板,而不使用多个持有者视图。这是我们提出的解决方案:

var myHolderView = Backbone.View.extend({
    el: '#views',
    render: function(){
        // This is because 'this' change inside the collection.each
        var $this = this;

        // If you want a wrapper template
        var wrapperHtml = $('#view-wrapper-template').tmpl();
        $this.$el.append(wrapperHtml);
        $wrapper = $this.$el.find('> div'); // If wrapper is a div


        $this.collection.each(function(model){
            // Render and append the viewA with internal events
            var viewA = new myViewA({model: model});
            viewA.render();

            // Use this to add the views content (viewA.el) to this views element ('#views')
            //$this.$el.append(viewA.el);

            // Use this to add the view content (viewA.el) to this views wrapper element ($wrapper)
            $wrapper.append(viewA.el);


            // Render and append the viewB with internal events
            var viewB = new myViewB({model: model});
            viewB.render();
            //$this.$el.append(viewB.el);
            $wrapper.append(viewB.el);


            // Add more views here...
        });
    }
});

完整源代码和工作示例:http: //jsfiddle.net/HLv5z/9/

于 2013-05-21T09:11:26.463 回答