2013-12-16 36 views
1

我遇到了骨幹問題,雖然所有腳本都已加載,但模型似乎對骨幹不明確。 (我正在使用require來加載主幹和其他JavaScript文件)。Backbone targetModel = undefined

所以每當我做一個collection.fetch我得到這個錯誤在Firebug:

TypeError: targetModel is undefined 

當我運行它擁有在這一點上腳本:

if (attrs instanceof Model) { 
    id = model = attrs; 
} else { 
    id = attrs[targetModel.prototype.idAttribute]; 
} 
當我和我的鼠標懸停

它說:undefined 它現在似乎不工作,我做的唯一的事情就是改變我的html模板,它只能在collection.fetch後加載。

你能幫我解決嗎?

這裏是我的模型:

var OF = OF || {}; 

OF.UsersMdl = Backbone.Model.extend({ 

    default: { 

     username: '', 
     mailinglist: '', 
     email: '' 

    }, 

    initialize: function() { 

     // 

    }, 

    result: { 
     success: false, 
     message: '' 
    }, 

    validate: function(att) { 

    } 

}); 

這裏是收集:

var OF = OF || {}; 

OF.UsersCollection = Backbone.Collection.extend({ 

    initialize: function() { 
     // 
    }, 

    parse: function(data){ 
     return data["all-users"]; 
    }, 

    model: OF.UsersMdl, 

    url: 'php/api/users' 

}); 

最後但並非最不重要的路由器與要求部分:

goToUsers: function() { 

    require(['./models/users', './views/users_view', './collections/user_collection'], function(UsersMdl, UsersView, UsersCollection) { 

     OF.usersMdl = new OF.UsersMdl; 
     OF.usersCollection = new OF.UsersCollection; 
     OF.usersView = new OF.UsersView; 

     //when the collection is fetched 
     $.when(OF.usersCollection.fetch({ 
      data: { 
       "admin": OF.login.attributes.admin, 
       "session": OF.login.attributes.session 
      }, 
      success: function(){ 
       //console.log(OF.usersCollection.length); 
      } 

     //then render the view 
     })).then(function(){ 

      OF.usersView.render(); 
     }, 300); 

    }); 

}, 

這裏是JSON將被取回:

{ 「所有用戶」: [ { 「用戶名」: 「測試儀」, 「郵件列表」: 「1」, 「電子郵件」: 「[email protected]」 },{ 「用戶名」: 「tester2」, 「郵件列表」: 「1」, 「電子郵件」: 「[email protected]」 },{ 「用戶名」: 「tester3」, 「郵件列表」:「0 「, 」email「:」[email protected]「 } ] }

在此先感謝

+1

定義了哪個targetModel? – fbynite

+0

targetModel是一個主幹變量。所以我沒有定義這個。除非你的意思是集合中的「model:OF.UsersMdl」部分? – BonifatiusK

+0

我個人從來沒有聽說過'targetModel'的默認變量,你可以發佈一個鏈接到文檔,它在哪裏被描述? – drax

回答

6

我有這個相同的錯誤,並對我的頭撞了很長一段時間,因爲骨幹對我來說是新的,這是一個取指問題。無論如何,我最終發現那個訂單很重要。衛生署! (我認爲使用CoffeeScript和「class」語句時不那麼明顯)。在我的模型之一中,我在Model之前設置了Collection(感謝來自Backbone.js on Rails書籍的糟糕示例代碼)。我扭轉了這一點,這個錯誤消失了,揭示了我真正的抓取問題。

同樣,您的model:屬性可能因此原因或其他原因無效,因此在稍後嘗試引用時會使其未定義。

附註:我在Backbone 1.0.0中有類似的錯誤。當我升級到Backbone 1.1.0時,我在主幹代碼的同一點得到了這個確切的錯誤。

+1

設置集合之前,我的模型實際上是我的問題,謝謝你的提示! D肯定幫助了我:D – AzurGroup

相關問題