2011-08-24 54 views
0

我有以下backbone.js代码,并在该事件中遇到问题,然后我从集合中触发“添加”事件。在success:提取中添加this.field.add(list_fields);导致错误。我如何确保模型被提取然后添加事件运行后Backbone.js添加事件问题

$(function() { 
     $(".chzn-select").chosen(); 
     /*********************************Models*************************************************/ 
     var Table = Backbone.Model.extend({ 

      urlRoot : '/campusfeed/index.php/welcome/generate' 
     }); 
     var Field = Backbone.Model.extend({ 
      urlRoot: '/campusfeed/index.php/welcome/generate' 
     }); 
     /**************************Collections*************************************************/  
     Tables = Backbone.Collection.extend({ 
      //This is our Friends collection and holds our Friend models 
      initialize : function(models, options) { 
       this.bind("add", options.view.addFriendLi); 
      //Listen for new additions to the collection and call a view function if so 
      } 
     }); 

     var Fields = Backbone.Collection.extend({ 
      model:Field, 
      url:'http://localhost/campusfeed/index.php/welcome/generateFields', 
      initialize : function(models, options) { 
       this.bind("add", options.view.getFields); 
      } 
     }); 
     /************************************Views**************************************************/ 
     var m="shit"; 
     var app = Backbone.View.extend({ 
      el:'body', 
      initialize:function(model,options){ 
       //Create collections in here 

       this.table = new Tables(null,{ 
        view : this 
       }); 
       this.field = new Fields(null,{ 
        view : this 
       }); 
      }, 
      events : { 
       "click #choose" : "generate" 

      }, 
      generate:function(){ 
       var table = (this.$("#table option:selected").text()); 
       var dbname = (this.$("#database").text()); 
       var list_fields = new Field(); 
       list_fields.urlRoot = list_fields.urlRoot+"/"+dbname+"/"+table; 
       list_fields.fetch({ 
        success:function(){ 
         console.log(JSON.stringify(list_fields)); 


        } 
       }); 

       this.field.add(list_fields); 

      }, 

      getFields:function(model){ 

       console.log(JSON.stringify(model)); 


      } 



     }); 
     var apprun = new app; 
    /* var data = new Fields(); 
     data.url=data.url+"/some/data"; 
     alert(data.url); 
     data.fetch(); 
     var staff = new Table(); 
     staff.fetch(); 
     var field = new Field();*/ 
    }); 

回答

1

问题是“this”的上下文。成功回调函数将“this”设置为list_fields。你可以用“self”或“that”变量解决这个问题:

 generate:function(){ 
      var table = (this.$("#table option:selected").text()); 
      var dbname = (this.$("#database").text()); 
      var list_fields = new Field(); 
      list_fields.urlRoot = list_fields.urlRoot+"/"+dbname+"/"+table; 
      var that = this; 
      list_fields.fetch({ 
       success:function(){ 
        console.log(JSON.stringify(list_fields)); 

        that.field.add(list_fields); 
       } 
      }); 
     }, 

作为一个附注 - 你的集合不应该有一个视图的引用。相反,您的视图应该引用收集并绑定到收集事件

var Fields = Backbone.Collection.extend({ 
     model:Field, 
     url:'http://localhost/campusfeed/index.php/welcome/generateFields', 
    }); 

    var app = Backbone.View.extend({ 
     initialize:function(model,options){ 
      //Create collections in here 

      this.field = new Fields(); 
      this.field.bind("add", this.getFields, this); 
     }, 
     getFields: function(){ ... } 
    }); 
+0

谢谢解决我的问题 – Madawar