更改变更模型事件的集合?

时间:2012-08-16 19:08:24

标签: javascript model-view-controller backbone.js

我有这个主干脚本,它有一个视图和一个充当控制器的模型,以及一个从服务器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'
        });
有人可以帮助我,谢谢你。           }        });

1 个答案:

答案 0 :(得分:0)

在骨干上下文中,View充当控制器(读取控制器形式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 
  }
});