1

我有以下backbone.js 脚本。传统上,我使用以下方法渲染视图:

var myItem = new Item();

myItem.fetch(); var reward_view = new ListView({model:myItem}); 奖励视图.render();

但是,我见过的大多数示例都没有这样做,并且以某种方式允许您使用以下方法加载/初始化视图:

var listView = new ListView();

如何?这是我尝试过的:

(function($){

   var Item = Backbone.Model.extend({
        urlRoot: '/api/v1/1/'
    });


  var List = Backbone.Collection.extend({
    model: Item
  });


  var ListView = Backbone.View.extend({

    el: '#reward_view',

       initialize: function(){
      _.bindAll(this, 'render'); // every function that uses 'this' as the current object should be in here

      this.collection = new List();
      this.render();

    },

     render: function(){
         console.log(this)
      $(this.el).html('<h1>Hello' + this.model.get('name') + '</h1>');
      return this; // for chainable calls, like .render().el
    }


  });
4

0 回答 0