2013-02-03 42 views
0

我有一個骨幹分頁程序集:骨幹分頁程序集不觸發渲染復位

var ExercisesCollection = Backbone.Paginator.requestPager.extend({ 

     model: ExerciseModel, 

     paginator_core: { 
      url: "/exercises" 
     }, 

     paginator_ui: { 
      firstPage: 1, 
      currentPage: 1, 
      perPage: 10, 
      totalPages: 10 
     }, 

     server_api: { 
      "filter": "", 
      "categories": "", 
      "top": function() { return this.perPage; }, 
      "skip": function() { return this.currentPage * this.perPage; } 
     }, 

     parse: function (response) { 
      this.totalPages = response.meta.totalPages; 
      return response.data; 
     } 

    }); 

我用它在一個骨幹視圖,像這樣:

var Exercises = Backbone.View.extend({ 

    initialize: function() { 
     this.collection = new ExercisesCollection(); 
     this.collection 
      .on("reset", this.render, this) 
      .on("change", this.render, this); 
     this.collection.goTo(1); 
    }, 

    render: function() { 
     alert("bah!"); 
    } 
}); 

通過查看網絡活動,我可以看到它正在向服務器發送請求並收到適當的響應。但它從不調用渲染功能。重置和更改事件似乎不起作用。

任何想法我做錯了什麼?

感謝

+0

this.collection你有沒有定義它? – salexch

+0

是this.collection是Paginator集合的一個實例 – user1031947

+0

.on(「add」,this.render,this)? – salexch

回答

1

我曾在2016年同樣的問題:)
當你使用'server'模式,你需要聽'sync'事件,不'reset'

// in a view 
this.listenTo(this.collection, 'sync', this.renderPage); 

從骨幹文檔(http://backbonejs.org/#Collection-fetch):

當從服務器模型數據的回報,它採用設置爲 (智能)合併所取得的模型,除非你通過{復位: 真},在這種情況下,集合將被(有效)重置。

所以它默認不會調用reset(),所以沒有'reset'事件。

我已經發現了這個答案'sync'事件: https://stackoverflow.com/a/17053978/1657101

爲骨幹1.0,model.fetch()觸發一個 '同步' 的。這就是你應該綁定的東西。