English 中文(简体)
形成收集的主干观点
原标题:Creating a backbone view for a collection

我怎么能够把主干的观点与收集而不是模型联系起来? 我是否需要以一个模式总结收集情况?

e.g.

如果我有一个主干模式客户,并收集这些所谓的客户

Client = Backbone.Model.extend({
    defaults: {
        Name:   
    }
});

Clients = Backbone.Collection.extend({
    model: Client,
    url:  Clients 
});

目 录

    var ClientListView = Backbone.View.extend({
        template: _.template($("#clients-template").html()),
        el: $( #clientlist ),

        initialize: function() {
            _.bindAll(this,  render );

            this.collection = new Clients();
        },

        render: function( event ){
            $(this.el).html(this.template({ this.collection.toJSON()));

            return this;
        }
    });

然后,我可以接触强调模板中的每个客户部分。 然而,如果我总结一下这种收集工作的话。

$(this.el).html(this.template({ clients: this.collection.toJSON() }));

然后,我可以。 这是否正确? 我会期望这是一个共同的设想,但我无法找到这方面的任何例子,我是否正视这一错误做法?

最佳回答

是的,你需要通过总结式收集。

http://github.com/addyosmani/backbone-fundamentals”rel=“nofollow”>Backbone Fundamentals 例,见follow?

认为:

$el.html( compiled_template( { results: collection.models } ) );

在模板中:

<% _.each( results, function( item, i ){ %>
   ...
<% }); %>

另一种选择是,对收集工作中的每一模式形成单独的看法。 http://liquidmedia.ca/blog/201102/backbone-js-part-3/“rel=”nofollow” An Intro to Backbone.js: Part 3 - Bled a Collection to a View:

var DonutCollectionView = Backbone.View.extend({
  initialize : function() {
    this._donutViews = [];
    this.collection.each(function(donut) {
      that._donutViews.push(new UpdatingDonutView({
        model : donut,
        tagName :  li 
      }));
    });
  },

  render : function() {
    var that = this;
    $(this.el).empty();

    _(this._donutViews).each(function(dv) {
      $(that.el).append(dv.render().el);
    });
  }
});
问题回答

您不妨研究





相关问题
What s the appropriate granularity for Backbone.js Views?

I m adopting Backbone.js to render a small corner of an existing large web app. If this goes well, I can see Backbone.js growing to encompass the whole of the app, lending some much-needed structure ...

Rendering Layouts with Backbone.js

If you were to build a single page web application (SPWA) using Backbone.js and jQuery with--for example--two controllers that each required a unique page layouts, how would you render the layout? ...

Load html without refresh

Im unsure of how to approach this, should I have the html to be loaded hidden or load it from somewhere? I want to load a form in one page, and dynamic content on other pages. The form can be saved ...

Why are my CoffeeScript/backbone.js events not firing?

I m trying to familiarize myself with CoffeeScript and backbone.js, and I must be missing something. This CoffeeScript: MyView = Backbone.View.extend events: { "click" : "testHandler" ...

热门标签