Object.implement Problem

49 views
Skip to first unread message

Peter

unread,
Sep 22, 2012, 2:54:37 PM9/22/12
to mootool...@googlegroups.com
I have this code with MooTools 1.4.5

Object.implement({
        getChecked: function() {  ... }
});
when i call it alone, then it works

but when i call

$('myElement').setStyles({'left':'10','top':'10'});

then Mootools create a JavaScript Error in SetStyles.
This i'm not understand. Is Object.implement not alowed ??

Sanford Whiteman

unread,
Sep 22, 2012, 4:52:15 PM9/22/12
to Peter
> This i'm not understand. Is Object.implement not alowed ??

You want Element.implement. Unless there's some application here that
I'm not understanding (you didn't talk about the purpose of this
function).

-- S.


Peter

unread,
Sep 23, 2012, 2:10:55 AM9/23/12
to mootool...@googlegroups.com

No, there is no right Element.implement.

I have a Function getChecked

function getChecked() {
    var count=0;
    var valuess = '';
    if(typeOf(el)=='elements') {
        el.each( function(el) {
            if(el.get('checked')) { 
                count++;
                if(valuess!="") valuess +=',';
                valuess += el.get('value');
            }
        });
    } else {
        if(el.get('checked')) { count=1; valuess=el.get('value'); }       
    }   
    return { 'count' : count, 'values':valuess } ;
}


var el = $$('myCeckboxes');

alert(getChecked(el));

The Idee is the function to use as implement for Object.

alert(el.getChecked().values);

It works when not a call for setStyles, but when i use set Styles, Mootools call the implemented Function as style ???

$('myDiv').setStyles({ 'left':'10px', 'top':'20px' });

then Mootools makes in Element.implement

setStyle( left ...
setStyle( top ...
setStyle( getChecked ...

I want to understand why this does not work.

Greetings,
and sorry for my bad english



Matthew Hazlett

unread,
Sep 23, 2012, 7:04:05 AM9/23/12
to mootool...@googlegroups.com
I'm not sure I understand your question. Is this what your trying to do?

http://jsfiddle.net/wTLXA/

Matthew Hazlett

unread,
Sep 23, 2012, 7:07:40 AM9/23/12
to mootool...@googlegroups.com
Made a typo, try this

http://jsfiddle.net/wTLXA/1/

On 9/23/2012 2:10 AM, Peter wrote:
>
1

Peter

unread,
Sep 23, 2012, 9:37:05 AM9/23/12
to mootool...@googlegroups.com
thank you, yes it is.  :)

but I would still like to know why my code works

Matthew Hazlett

unread,
Sep 23, 2012, 11:52:21 AM9/23/12
to mootool...@googlegroups.com
If you really want to write it that way:

Element.implement('getChecked', function(){
return $$('input[type=checkbox]:checked');
}
);

document.id('run').addEvent('click', function(){
alert( Element.getChecked() );
});

gonchuki

unread,
Sep 24, 2012, 12:00:48 PM9/24/12
to mootool...@googlegroups.com
Peter, the problem with your code is that Object.implement is the same as doing Object.prototype extensions, and those should *never* be done as anything you get into the Object prototype will show up when you do a for...in loop on any plain object. Most of MooTools internal loops are not guarded against Object.prototype extensions (using hasOwnProperty) so that's why the setStyles call is failing once it encounters your getChecked extension.
Reply all
Reply to author
Forward
0 new messages