1

I am updating to my earlier question based on the comment to get better understanding:

//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);

I am getting following error for wrapperModel and I am not able to create it's object.

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

So is it possible to do in backbone.js??

astra03
  • 505
  • 1
  • 7
  • 18
  • 1
    the code should work fine. What problems are you getting ? you could also abstract it a bit more by passing class and initializing it inside the constructor as in `model: Model(CollectionA)` & `this.data = new obj()` –  Apr 02 '13 at 19:17
  • Do you have an use case that will show the use of a different interface to those collections? Otherwise dynamic typing is much about ignoring type until the last moment. – snedkov Apr 02 '13 at 19:18
  • I tried that John but its giving me error. I have updated the code. let me know if I am making any mistake's. – astra03 Apr 02 '13 at 20:40

1 Answers1

1

I think you're overthinking it. The model attribute of Backbone Collection are not THAT useful. You basically won't need them if you're not fetching anything. If you are though, I think you missed something in Backbone's doc:
"A collection can also contain polymorphic models by overriding this property with a function that returns a model."

Oh, and your error comes from thoses lines:

model: WrapperModel(new Students()),

Now, why doesn't this work is pretty crystal clear but requires some knowledge about JavaScript objects//classes: using a class as a function WONT create an object, therefore your context IS NOT the class NOR an instance of it. In this case, the this will be the global object as your error reads, and Backbone needs an object context to work correctly.
You could also try new WrapperModel(new Students()). You'd have no error, but this wouldn't work. The model attribute expects a class.

tl;dr: your line of code doesn't make any sense.

Loamhoof
  • 8,293
  • 27
  • 30
  • Is there any I can achieve this in backbone.js or need to stick with whatever I was doing like having different WrapperModel class each time... Let me check the documentation once again – astra03 Apr 03 '13 at 17:09