2017-10-17 139 views
0

所以我學習backbonejs,使用nodecellar-rethinkdb例如作爲實踐分頁錯誤

更新Backbone.js的和Underscore.js文件的最後一個版本後,我發現了一個錯誤說「頁面沒有定義」。相同的代碼與Backbone.js 0.9.2很好地工作。所以我想一些東西被棄用。

這裏誰是給我的錯誤分頁文件的代碼:

window.WineListView = Backbone.View.extend({ 

    initialize: function() { 
     this.render(); 
    }, 

    render: function() { 
     var wines = this.model.models; 
     var len = wines.length; 
     var startPos = (this.options.page - 1) * 8; 
     var endPos = Math.min(startPos + 8, len); 

     $(this.el).html('<ul class="thumbnails"></ul>'); 

     for (var i = startPos; i < endPos; i++) { 
      $('.thumbnails', this.el).append(new WineListItemView({model: wines[i]}).render().el); 
     } 

     $(this.el).append(new Paginator({model: this.model, page: this.options.page}).render().el); 

     return this; 
    } 
}); 

window.WineListItemView = Backbone.View.extend({ 

    tagName: "li", 

    initialize: function() { 
     this.model.bind("change", this.render, this); 
     this.model.bind("destroy", this.close, this); 
    }, 

    render: function() { 
     $(this.el).html(this.template(this.model.toJSON())); 
     return this; 
    } 

}); 

能否請你給我爲什麼代碼不能與去年的版本工作的原因,並修復它與最新的骨幹工作.js

在此先感謝。

回答

0

嘗試保持參考你自己,我不認爲新版本的骨幹保持它。

Backbone.View.extend({ 

initialize: function (options) { 
    //add this 
    this.options = options; // or this.page = options.page and update render accordingly 
    this.render(); 
}, 
+0

謝謝!添加兩個作品:'initialize:function(options){this.options = options; this.page = options.page; this.render(); },' – RocketFuel

+0

但是使用兩個選項都可以嗎?或者我可以修復它只使用1?因爲當我使用只是:this.page = options.page;我得到一個錯誤,說沒有定義選項 – RocketFuel

+0

@RocketFuel當你使用第二個時,你需要更新以下行,如$(this.el).append(new Paginator({model:this.model,page:this.page}) .render()。el);'理想情況下,只要視圖存在於內存中,就不需要保留對整個選項對象的引用,所以只需保留'this.page'就足夠了 –