2012-08-16 38 views
0

我有這個骨幹腳本,它有一個單一的視圖和作爲控制器的模型,以及從服務器url: '/search/:term'獲取數據的集合。重置更改模型事件的集合?

var Items = Backbone.Collection.extend({ 
      initialize: function(terms){ 
      this.fetch(); 
     } 
      url: 'search/:term' 
    }); 

    var Controller = Backbone.Model.extend({ 
     defaults:{ 
     term: "" 
    }, 
     initialize: function(opts){ 

     this.on('change:term', function(term){ 
      console.log(this.get("term")); 
      // every time term changes i want to refresh the collection with the new data 
      // so it will fetch data from url:'search/ + term' 
     }); 

有人可以幫我這個謝謝。 } });

+0

你能證明你試圖達到什麼嗎?如果你試圖聽一些dom事件,你應該使用Backbone.View。在主幹上下文中,View充當Controller。 – 2012-08-16 19:14:10

回答

0

在主幹上下文中,View用作Controller(讀取控制器表單mvc)。你可以嘗試做這樣的事情嗎?

YourView = Backbone.View.extend({ 
    events : { 
    'change #dom-id' : 'handleChange' //function to fire when you change the dom 
    }, 
    initialize: function(){ 
    this.collection.bind("reset", updateView); //when collection is done, updateView fires 
    }, 
    render: function(){ 
    //do your render logic here, use a template etc.. //initial rendering 
    }, 
    handleChange : function(changeEvent){ 
    //get value from dom-element, tell the collection to 
    //fetch again with the new term 
    }, 
    updateView : function(){ 
    //Code that fires when the collection is done fetching 
    } 
}); 
+0

我如何更改網址! – user1551482 2012-08-16 20:24:01

+0

添加功能到您的收藏: 搜索:功能(term){ this.url ='search /'+ term; this.fetch(); }, – 2012-08-16 20:30:23

相關問題