Yeah just use regular adopt forbarry's example... but i'm trying to do
something different:
$('moo').conditionalAdopt( condition, el1, el2, el3... )
Where conditonalAdopt evaluates the first parameter in terms of true/
false -- then adopts the remaining elements only if the first
parameter was indeed true.
This is particularly relevant if you want to reuse a large create
element method like this:
var foo = new Class({
initialize:function(){
var _this = this;
$('blah').addEvent('keyup', function(){
this.adopt(_this.createElement(this.value));
});
},
createElement: function(value){
return new Element('div').adopt(
new Element('div'),
new Element('div'),
new Element('div').conditionalAdopt(
value.test('moo'), //if value has moo in it, then adopt
the following elements
new Element('div',{
'html': value
}),
new Element('div',{
'html': value
})
)
);
}
});
what usually happens is you have to abstract that portion out into its
own function... and put your conditional there, leaving you with
something like:
var foo = new Class({
initialize:function(){
var _this = this;
$('blah').addEvent('keyup', function(){
this.adopt(_this.createElement(this.value));
});
},
createElement: function(value){
return new Element('div').adopt(
new Element('div'),
new Element('div'),
new Element('div').adopt(
this.createSubStructure(value)
)
);
},
createSubStructure: funciton(value){
if(value.test('moo')){
return [new Element('div',{
'html': value
}),
new Element('div',{
'html': value
})];
}
return null;
}
});
On Mar 30, 5:35 pm, Jan Kassens <[email protected]> wrote:
> Can'’t you just use myElem.adopt(elema, elemb, elemc);? I think it skips
> null/false ‘elements’.
>
> On Tue, Mar 30, 2010 at 11:58 AM, Barry van Oudtshoorn <
>
> [email protected]> wrote:
> > Well, writing
>
> > myElem.conditionalAdopt(elema, elemb, elemc);
>
> > is a couple of characters shorter than writing
>
> > if (elema) {
> > myElem.grab(elema);
> > }
> > if (elemb) {
> > myElem.grab(elemb);
> > }
> > if (elemc) {
> > myElem.grab(elemc);
> > }
>
> > (even when minified), so if you use this pattern a lot, it could pay off.
> > :)
>
> > Pragmatically, to reduce DOM manipulation, wouldn't this implementation of
> > the function be somewhat more efficient:
>
> > Element.implement({
> > conditionalAdopt: function() {
> > var els = [];
> > $A(arguments).each(function(item) {
> > item = document.id(item);
> > if (item) els.push(item);
> > }
> > return this.adopt(els);
> > }
> > });
>
> > On 30/03/2010 5:23 PM, Oskar Krawczyk wrote:
>
> > ... on the web where every byte counts.
>
> > Haven't heard anyone saying that since 1999 - you know, 56K modems and
> > all :-)
>
> >> 2010/3/29 jacob <[email protected]>
>
> >> Anyone ever implemented something like this? or have a nicer way of
> >>> doing it?
>
> >>>http://mootools.net/shell/KPPNg/
>
> >>> Element.implement({
>
> >>> conditionalAdopt: function(){
> >>> if(Array.prototype.splice.apply(arguments,[0,1])[0]){
> >>> Array.flatten(arguments).each(function(element){
> >>> element = document.id(element, true);
> >>> if (element) this.appendChild(element);
> >>> }, this);
> >>> return this;
> >>> }
> >>> return this;
> >>> }
>
> >>> });
--
To unsubscribe, reply using "remove me" as the subject.