Tôi đang cố gắng hiểu mối quan hệ giữa một mô hình và chế độ xem. Tôi đã thử xây dựng mô hình và chế độ xem để hiển thị mô hình đó.nhận dữ liệu mô hình cho chế độ xem backbone.js
Tôi gặp lỗi Cannot call method 'toJSON' of undefined
mà tôi hiểu là phiên bản thực tế của mô hình không được gửi đến chế độ xem.
Tôi cảm thấy có điều gì đó bị thiếu trong quá trình khởi tạo chế độ xem?
Model:
var sticky = Backbone.Model.extend({
defaults: {
title:"",
content:"",
created: new Date()
},
initialize: function() {
console.log("sticky created!");
}
});
The View:
var stickyView = Backbone.View.extend({
tagName:"div",
className:"sticky-container",
initialize: function() {
this.render();
console.log("stickyView created!");
},
render: function() {
$("#content-view").prepend(this.el);
var data = this.model.toJSON(); // Error: Cannot call method 'toJSON' of undefined
console.log(data);
var source = $("#sticky-template").html();
var template = Handlebars.compile(source);
$(this.el).html(template(data));
return this;
}
});
Tạo mô hình mới và thể hiện mới của quan điểm:
var Sticky = new sticky({title:"test"});
var StickyView = new stickyView();
Ah bây giờ tôi nhận được nó, cảm ơn vì những lời khuyên bổ sung quá. – salmoally