私はどこでも答えを探しましたが、私が見つけたものには満足していませんでした。バックボーンthis.modelは定義されていません、なぜですか?
問題は、Addy Osmaniのバックボーンで「Todo」アプリケーションを作成するチュートリアルですが、コンソールを見ると、this.model is undefined
というエラーが表示されます。
私はこのSOの答えを試したこともあります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
});
私は間違っているかもしれませんが、 'todos'を' model'ではなく 'collection'としてビューに渡したいと思うので、' this.collection'としてビュー内で参照するようにします –