一個問題,我是新來的骨幹,我試圖完成一些簡單的任務,比如從模型渲染名稱列表。但我得到這個錯誤:經與骨幹查看
'cannot read property "model" of undefined'
我真的很感謝任何幫助,以及任何提示一般。
var Student = Backbone.Model.extend({
getConvertedToHash: function() {
return $.extend({}, this.attributes.student[0], this.attributes.student[1], this.attributes.student[2]);
}
});
var Group = Backbone.Collection.extend({
model: Student,
initialize: function() {
this.add(new Student({
student: [
{
"name": "john",
"lastName": "fox",
"middleName": "yonson"
},{
"age": 26,
"gender": "male"
},{
"passport": "qpuid5423",
"inn": 123542
}]
}));
this.add(new Student({
student: [
{
"name": "john",
"lastName": "fox",
"middleName": "yonson"
},{
"age": 26,
"gender": "male"
},{
"passport": "qpuid5423",
"inn": 123542
}]
}));
}
});
var StudentView = Backbone.View.extend({
tagName: 'li',
className: 'alert alert-info',
initialize: function() {
_.bindAll(this, 'render');
},
render: function() {
this.$el.html(this.model.getConvertedToHash().name);
return this;
}
});
var GroupView = Backbone.View.extend({
el: $('body'),
initialize: function() {
_.bindAll(this, 'render');
this.group = new Group();
this.render();
},
render: function() {
var $ul = $('<ul>').addClass('student-list');
_.each(this.group.models, function (element, i) {
var studentView = new StudentView({
model: this.group.models[i]
});
$ul.append(studentView.render().el);
});
thi.$el.append($ul);
}
});
var groupView = new GroupView();
我需要在學生模型怪方法getConvertedHash()
這樣我就可以得到一個哈希值,而不是對象的數組(如我的初步數據結構:這需要進一步的目的)。
的錯誤是因爲你試圖訪問一個未定義的對象的屬性'model' 。例如,如果你鍵入'this.group.model'和'this.group'不存在,你會得到這個錯誤。我在代碼中看不到這種情況的證據,所以我相信這個錯誤在別處。如果你可以提供一個jsbin或小提琴你的代碼不起作用,那會有所幫助。 – Jan