2013-07-02 109 views
1

我使用的骨幹 - 關係,我收到以下錯誤:遺漏的類型錯誤:無法讀取屬性「idAttribute」的未定義

Uncaught TypeError: Cannot read property 'idAttribute' of undefined 

當我訪問show_note路線。

App.Router = Backbone.Router.extend({ 
    routes: { 
    'note/:_id': 'show_note' 
    }, 

    show_note: function(_id) { 
    console.log('this is the show_note route for _id: ' + _id); 
    var note = new App.Models.Note.findOrCreate({ _id: _id }); 
    var note_view = new App.Views.main_note({ model: note }); 
    note.fetch(); 
    } 
}); 

console.log收到'_id'。但是當我嘗試實例化var note時,我收到錯誤。我該如何解決?

EDIT 1

添加註模型:

App.Models.Note = Backbone.RelationalModel.extend({ 
    urlRoot: '/notes', 
    idAttribute: '_id', 
    relations: [{ 
    type: Backbone.HasMany, 
    key: 'tags', 
    relatedModel: 'App.Models.Tag', 
    reverseRelation: { 
     key: 'note', 
     includeInJSON: '_id' 
    } 
    }] 
}); 

編輯2

堆棧跟蹤加入

enter image description here

+0

能否請您出示Models.note定義是什麼? – damienc88

+0

你有全球範圍內的App.Models.Tag嗎? –

+0

@ muistooshort是 –

回答

0

我通過移除findOrCreate()解決問題,結束了檢索,像這樣的模型數據:

App.Router = Backbone.Router.extend({ 
    routes: { 
    'note/:_id': 'show_note' 
    }, 

    show_note: function(_id) { 
    var note = new App.Models.Note({ _id: _id }); 
    note.fetch({ 
     success: function(data) { 
     var mainNoteView = new App.Views.main_note({ model: note }); 
     $('#mainNote').append(mainNoteView.render().el); 
     }, 
     error: function(err) { 
     console.log(err); 
     } 
    }); 
    } 
}); 
相關問題