1

我正在根据评论更新我之前的问题,以便更好地理解:

//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中做吗?

4

1 回答 1

1

我觉得你想多了。Backbone Collection 的模型属性没有多大用处。如果您不获取任何内容,则基本上不需要它们。如果你是,我认为你错过了Backbone 的文档中的一些内容:
一个集合也可以包含多态模型,方法是用一个返回模型的函数覆盖这个属性。

哦,你的错误来自那些行:

model: WrapperModel(new Students()),

现在,为什么这项工作不是非常清楚,但需要一些关于 JavaScript 对象//类的知识:使用类作为函数不会创建对象,因此您的上下文不是类,也不是它的实例。在这种情况下,this将是您的错误读取的全局对象,并且 Backbone 需要一个对象上下文才能正常工作。
你也可以试试new WrapperModel(new Students())。你没有错误,但这不起作用。模型属性需要一个类。

tl; dr:您的代码行没有任何意义。

于 2013-04-03T08:11:34.453 回答