mootools variable scope -
how access outer function's argument 'parent' ??? please see comments in code !!
last edit : question misleading, problem caused wrong input argument
renderdata : function(parent, children){ children.each(function(e, index){ var li = new element('li'); var haschildren = false; if(e.children && e.children.length >0){ var img = new element('img'); img.src = 'a1.png'; img.inject(li); haschildren = true; } if(e.icon){ var img = new element('img'); img.src = e.icon; img.inject(li); }else{ var img = new element('img'); img.src = 'b1.png'; img.inject(li); } li.set('html',e.text); console.log(this); // how access outer function's argument 'parent' ??? li.inject(parent); if(haschildren){ var ul = new element('ul'); this.renderdata(ul, e.children); ul.inject(e); } }.bind(this));
within each loop:
array.each(function(el) { this.method(); // == (instance / scope) }, this); // **this** parent scope.
another acceptable way is:
var self = this; ... array.each(function(el) { self.method(); // fine. }); // parent scope.
http://mootools.net/docs/core/types/array#array:array-each
although, using .bind(this)
should work too... http://www.jsfiddle.net/dimitar/ffy4j/ - problem?
Comments
Post a Comment