arrays - Javascript - Collection of objects with parent properties in common -


i'd create collection of objects works array. time ago, made this question , came following solution of people helped me:

newobj.collection = function(){     array.apply(this);      for(var = 0; < arguments.length; i++){         for(var j = 0; j < arguments[i].length; j++){             this.push(arguments[i][j]);         }     }      return }  newobj.collection.prototype = object.create(array.prototype); newobj.collection.prototype.push = function(o){     array.prototype.push.call(this, new newobj.element(o)); } 

however, leaves children unconnected parent. example, imagine collection has render() function, makes children print html onto page. well, i'd able like:

newobj.collection.html_container = '#cont';  newobj.collection.render = function(){     $.each(this, function(i, el){         el.render()     }) }  newobj.element.render = function(){     $(parent.html_container).html('.......') } 

it should able set different collections in 1 page, make global container newobj.collections not solution. example, , need more complex processes render() function.

anyone has idea how can make array able access parent class part of?

if solution json.stringifyed , seen array on server side, great too, though it's not main problem question. right now, if set property array, seen object size > 0 on server side.

thank you!

create reference collection in element:

newobj.collection.prototype.push = function(o){   array.prototype.push.call(this, new newobj.element(o,this)); }  //element constructor gets second paramater instance of collection newobj.element=function(o,collection){    //this.parent in every element collection reference   this.parent=collection; }   newobj.element.prototype.render = function(){    $(this.parent.html_container).html('.......') } 

or no reference in element option:

newobj.collection.render = function(){    var parent=this;    $.each(this, function(i, el){     el.render(parent.html_container)   }) }  newobj.element.render = function(html_container){   $(html_container).html('.......') } 

but version need have methods parameters.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -