2013-07-02 111 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); 
     } 
    }); 
    } 
}); 
相关问题