javascript - Reference (not copy) a class as a member of another Class - Mootools -


i have following classes

the box class

var box = new class({     implements: [options],     options: {         name: 'new',         weight: 0     },     initialize: function (options) {         this.setoptions(options);     },     getparent: function () {         return this.options.parent;     } }); 

the collection class

var collection = new class({     implements: [options],     options: {         boxes: []     },     boxes: [],     initialize: function (options) {         var self = this;         this.setoptions(options);         array.each(this.options.boxes, function (box) {             self.boxes.push(new box({                 parent: self,                 name: box.name,                 weight: box.weight             }));         });     } }); 

i passing collection class (as parent) box class when created.

var newcollection = new collection({     boxes: [         {             name: 'a',             weight: 9         },          {             name: 'b',             weight: 3         },          {             name: 'c',             weight: 2         },          {             name: 'd',             weight: 5         },          {             name: 'e',             weight: 7         }     ] }); 

i want parent in box class reference collection class , not copy, although seems copy of newcollection class each time box class created (the length of boxes different each one)

array.each(newcollection.boxes, function (box) {     console.log('*',box.getparent()); }); 

i new mootools , though have gone through documentation, way ended writing code. there more accepted coding pattern in mootools can reference parent?

here's fiddle.

easy miss. setoptions (docs) deep copies options object. set parent property of box after initializing it. i've posted modified version of code here

array.each(this.options.boxes, function (box) {     var nb = new box({         name: box.name,         weight: box.weight     });     // note difference     nb.parent = self;     self.boxes.push(nb); }); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -