我正在根据评论更新我之前的问题,以便更好地理解:
//Model's
Student = Backbone.Model.extend({
defaults: {
name: ''
}
});
Students = Backbone.Collection.extend({
model: Student,
initialize: function(){
},
parse: function(resp) {
return resp;
}
});
Teacher = Backbone.Model.extend({
defaults : {
name : '',
salary : ''
}
});
Teachers = Backbone.Collection.extend({
model: Teacher,
initialize: function(){
},
parse: function(resp) {
return resp;
}
});
/**
* I Just need WrapperModel only single class instead of two so that when ever I
* make collection of this class I can dynamically bind the 'data' attribute to
* either Teachers or Students
*/
WrapperModelStudent = Backbone.Model.extend({
defaults : {
message : '',
processingStatus : ''
},
initialize : function() {
this.data = new Students([new Student({name: "Marry"}),
new Student({name: "Susan"}),
new Student({name: "Samanta"})
]);
}
});
WrapperModelTeacher = Backbone.Model.extend({
defaults : {
message : '',
processingStatus : ''
},
initialize : function() {
this.data = new Teachers();
}
});
WrapperModelStudents = Backbone.Collection.extend({
model: WrapperModelStudent,
initialize: function(){
},
parse: function(resp) {
return resp;
}
});
WrapperModelTeachers = Backbone.Collection.extend({
model: WrapperModelTeacher,
initialize: function(){
},
parse: function(resp) {
return resp;
}
});
/**
*Trying below
*/
/***
* instead of using above two need to just use below one. How to do this??
*/
WrapperModel = Backbone.Model.extend({
defaults : {
message : '',
processingStatus : ''
},
initialize : function(obj) {
this.data = obj;
}
});
WrapperModelStudentsA = Backbone.Collection.extend({
model: WrapperModel(new Students()),
initialize: function(){
},
parse: function(resp) {
return resp;
}
});
WrapperModelTeachersB = Backbone.Collection.extend({
model: WrapperModel(new Teachers()),
initialize: function(){
},
parse: function(resp) {
return resp;
}
});
wrapperModelStudent = new WrapperModelStudent({message:"success",processingStatus:"y"});
wrapperModelStudents = new WrapperModelStudents([wrapperModelStudent]);
wrapperModelTeacher = new WrapperModelTeacher({message:"success",processingStatus:"y"});
wrapperModelTeachers = new WrapperModelTeachers([wrapperModelTeacher]);
wrapperModel = new WrapperModel({message:"success",processingStatus:"y"});
wrapperModelStudentsA = new WrapperModelStudentsA([wrapperModel]);
wrapperModelTeachersA = new WrapperModelTeachersA([wrapperModel]);
console.log(wrapperModelStudents);
console.log(wrapperModelTeachers);
我收到 wrapperModel 的以下错误,我无法创建它的对象。
Uncaught TypeError: Object [object global] has no method 'set'
*************** backbone-min.js:10
g.Model
*************** backbone-min.js:10
d
*************** backbone-min.js:38
(anonymous function) sample.html:110
那么有可能在backbone.js中做吗?