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'
        });

有人可以帮我解决这个问题吗?谢谢。} });

4

1 回答 1

0

在主干上下文中,视图充当控制器(从 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 
  }
});
于 2012-08-16T19:22:59.830 回答