私はどこでも答えを探しましたが、私が見つけたものに満足していませんでした.
問題は、Backbone で「Todo」アプリを作成するために Addy Osmani のチュートリアルを行っているのですが、コンソールを見ると、というエラーが表示されることthis.model is undefined
です。
私もこのSO answer Backbone model error displayed in consoleを試しましたが、それでも同じエラーが発生します。何が悪いのか教えてください。
ところで、this.model
またはは何this.collection
ですか?それらが参照しているアイデアがBackbone.Model
ありBackbone.Collection
ますが、どのように機能しますか? this.collection
別のチュートリアルで とを明確に定義しthis.model.models
たときに、 も未定義だったので、これを尋ねています。Model
Collection
どうもありがとう
JS:
//Model
var Todo = Backbone.Model.extend({
defaults: {
title: 'Enter title here',
completed: true
},
validate: function(attrs) {
if (attrs.title === undefined) {
return 'Remember to enter a title';
}
},
initialize: function() {
console.log('This model has been initialized');
this.on('change:title', function() {
console.log('-Title values for this model have changed');
});
this.on('invalid', function(model, error) {
console.log(error);
});
}
});
//View
var TodoView = Backbone.View.extend({
el: '#todo',
tagName: 'li',
template: _.template($('#todoTemplate').html()),
events: {
'dbclick label': 'edit',
'click .edit': 'updateOnEnter',
'blur .edit': 'close'
},
initialize: function() {
_.bindAll(this, 'render');
this.render();
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
this.input = this.$('.edit');
console.log(this.model.toJSON());
return this;
},
edit: function() {
//do something...
},
close: function() {
//do something...
},
updateOnEnter: function() {
//do something...
}
});
var todoview = new TodoView();
console.log(todoview.el);
//Collection
var TodoList = Backbone.Collection.extend({
model: Todo
});