मैंने हर जगह एक उत्तर के लिए देखा है लेकिन मुझे जो मिला है उससे संतुष्ट नहीं था।बैकबोन में यह.मोडेल अनिर्धारित है, क्यों?
मुद्दा यह है कि, मैं एडी ओस्मानी से बैकबोन में 'टोडो' ऐप बनाने के लिए एक ट्यूटोरियल कर रहा हूं, लेकिन जब मैं कंसोल को देखता हूं, तो मुझे 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
परिभाषित किया है।
कई धन्यवाद
जे एस:
//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' को' संग्रह 'के रूप में देखने के लिए' संग्रह 'और' मॉडल 'के रूप में पारित करना चाहते हैं, ताकि आप इसे' this.collection' –