2012-03-13 41 views
0

我從Backbone.js的0.5.3升級到0.9.1,我有在這裏一個特殊的錯誤問題,是回溯追蹤:骨幹網的升級問題

**Uncaught TypeError: object is not a function** 
_.extend._prepareModel 
_.extend.add 
_.extend.reset 
Backbone.Collection 
child 
Backbone.View.extend.render 
(anonymous function) 
Backbone.Events.trigger 
stage.$el.stop.animate.complete 
jQuery.extend.speed.opt.complete 
jQuery.fx.step 
t 
jQuery.extend.tick 

當我去的代碼並研究它,它似乎是從一個集合,但最終的誤差點到來是:

// Prepare a model or hash of attributes to be added to this collection. 
    _prepareModel: function(model, options) { 
     options || (options = {}); 
     if (!(model instanceof Model)) { 
     var attrs = model; 
     options.collection = this; 
     model = new this.model(attrs, options); 
    /
    /
    /
    here: Uncaught TypeError: object is not a function 



     if (!model._validate(model.attributes, options)) model = false; 
     } else if (!model.collection) { 
     model.collection = this; 
     } 
     return model; 
    }, 

更新:

如這裏要求的步驟是:

//from inside the collection (this object is actually coming in from elsewhere but 
var viewconfig ={ 
      id:"values-tab-panel", 
      idprefix:"values-tab", 
      classname:"tabpanel", 
      items:[ 
       { 
        urlRoot:"/"+lang+"/values", 
        url:"someurl1" 

       }, 
       { 
        urlRoot:"/"+lang+"/values", 
        url:"/someurl2" 

       }, 
       { 
        urlRoot:"/"+lang+"/values", 
        url:"/someurl3", 
       } 
      ]} 
    this.view = new TabPanelView(viewconfig); 

,那麼該觀點的內部:

render: function(args){ 
     // 
     /* 
     * what needs to happen is that the first time the render is called it creates a jquery DOM object which 
     * can then be manipulated hencforth, currently there's all kinds of javscript bits which relate to it but not 
     * an actual piece of DOM. closure should deal with the rest of it but theres nothing which is assigned 
     * 
     */ 
     if(!args) 
     { 
      //first render 
      var nav    = $("<aside/>").addClass("tab-navigation").append("<ol/>").attr("role","navigation"); 
      var tabcontent  = $("<section/>").addClass("tab-panels"); 
      for(i = 0;i<this.views.length;i++) 
      { 
       $("ol",nav).append("<li><a rel='"+this.views[i].id+"' href='javascript:;' class='tab-nav'></a></li>"); 
       tabcontent.append(this.views[i].el); 
      } 
      this.$el.empty().append(nav).append(tabcontent); 
      //this.$el.append("<aside class='tab-navigation' ><ol role='navigation'>"+listhtm+"</ol></aside>") 
      //this.$el.append("<section class='tab-panels'>"+innerhtm+"</section>"); 

      this.attach(); 
     } 
     else if(args && args.update == true){ 
      // partial render -- i.e. update happening 

      this.container = $(this.id); 
      var targetid = args.what.cid; 
      for(i = 0;i<this.views.length;i++) 
      { 
       var curcontent = this.$el.find("div#"+this.views[i].id); 
       var curlink  = this.$el.find("a[rel='"+this.views[i].id+"']") 
       if(this.views[i].cid == targetid) 
       { 
        curcontent.html($(this.views[i].el).html()); 
        curlink.text(this.views[i].model.rawdata.header); 
       } 
       if(i>0) 
       { 
        // set the first panel 
        curcontent.addClass("tab-content-hide"); 
       } 
       if(i==0) 
       { 
        curcontent.addClass("tab-content-show"); 
        curlink.addClass("tab-nav-selected"); 
       } 
       //$("a[rel='"+this.views[i].id+"']").die().unbind().live("mousedown",this.switchtabs);// dont ask 
       log("a[rel='"+this.views[i].id+"']") 
      } 

      this.update(); 
     } 
     return this; 
    }, 
+1

你能至少顯示「Backbone.View.extend.render」功能? – 2012-03-13 20:14:37

+1

您是否在該行上放置了一個斷點以查看'this.model'是?你是否追溯過來看看'this.model'是從哪裏來的,以確保你確實傳遞了一個Backbone模型的實例?這是非常模糊的,而不無論是與例如破的jsfiddle,或到現場 – tkone 2012-03-13 20:49:51

+0

@tkone的鏈接來解決,我完全同意,但代碼庫是巨大的。我想我已經跟蹤它..努力工作了幾位出 – Alex 2012-03-13 20:53:53

回答

1

我還不能完全肯定,如果這是它打消了我的錯誤,並且使事情現在進展順利權UT。

看起來好像Backbone 0.9.x已經做到了,所以你不能在Collection的initialize函數中設置model。對於我的錯誤是,在骨幹0.5.xi可以這樣做:

var mycollection = Backbone.Collection.extend({ 
    initialize: function(args) 
    { 
     this.model = new mycollectionmodel(); 
       //some stuff 
     } 
}); 
var whatever = new mycollection(); 
然而

在骨幹0.9.xi不得不這樣做:

var mycollection = Backbone.Collection.extend({ 
     model: mycollectionmodel, 
    initialize: function(args) 
    { 
     //some stuff 
     } 
}); 
var whatever = new mycollection(); 

或本:

var mycollection = Backbone.Collection.extend({ 
    initialize: function(args) 
    { 
     //some stuff 
     } 
}); 
var whatever = new mycollection({model:new mycollectionmodel()}); 

但第一個拋出上述錯誤...

+0

你爲什麼試圖在初始化方法中設置你的模型?實例化您的集合之前,您的模型是否存在? – tkone 2012-03-13 22:02:34

+0

是的,它是一種公用事業父集合,我有一個非常相似的集合,它有點差異(基本上不同類型的標籤式內容)的負載。我已經放棄了動態模型,並將其作爲第二好的基礎 – Alex 2012-03-13 22:21:39