2012-03-13 52 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