Element.implement

101 views
Skip to first unread message

Steve Onnis

unread,
Jul 14, 2011, 9:29:34 PM7/14/11
to mootool...@googlegroups.com

Can you implement things on particular elements only? Or do you have to check the type of element in the implements function and deal with it there?

 

Steve

Ryan Florence

unread,
Jul 14, 2011, 11:16:44 PM7/14/11
to mootool...@googlegroups.com
You might have asked "Can you implement methods on only certain types of elements"

You'll need to do this.get('tag') and check against it in your method.

And you might have asked "Can you implement methods on a particular element".

In this case, simple append a method to the element.

var el = $('el');
el.whatev = function (){};

Steve Onnis

unread,
Jul 14, 2011, 11:29:05 PM7/14/11
to mootool...@googlegroups.com

Like if i only want to implement something on a table element for example

Ryan Florence

unread,
Jul 15, 2011, 12:01:41 AM7/15/11
to mootool...@googlegroups.com
afaik you just do this.get('tag') and check it.

I'd just implement it and plan on using it on just table elements, I wouldn't bother with the check.

Steve Onnis

unread,
Jul 15, 2011, 12:24:50 AM7/15/11
to mootool...@googlegroups.com

Thats what i have been doing but i thought i would ask the question anyway

Sanford Whiteman

unread,
Jul 15, 2011, 1:27:33 AM7/15/11
to mootool...@googlegroups.com
If the browser exposes and allows you to further extend HTMLElement extensions such as HTMLTableElement, then yes. But I wouldn't bother with it because you're not going to get full BC and have to implement get('tag') as a failsafe anyway.

If you plan to use document.id() everywhere (thus assuming no extended natives at all) you could add the tagName check inside the wrapper and then make it seem transparent to the outside. This, too, is a waste of time IMO. Just use get('tag') if you want this feature to be compatible with all Moo-supported browsers.

-- Sandy

Steve Onnis

unread,
Jul 15, 2011, 1:44:57 AM7/15/11
to mootool...@googlegroups.com
The reason i asked is because i was looking into some custom form element
styling functions so i wanted to be able to do something like

Select.style()
Input.style()
Radio.style()

And so on but i will most likely have to do it a different way

-----Original Message-----
From: Sanford Whiteman [mailto:sa...@cypressintegrated.com]
Sent: Friday, 15 July 2011 3:28 PM
To: mootool...@googlegroups.com
Subject: Re: [Moo] Element.implement

Aaron Newton

unread,
Jul 15, 2011, 3:44:50 AM7/15/11
to mootool...@googlegroups.com

var el = $('el');
el.whatev = function (){};

Careful - this is how you leak memory in numerous browsers if you aren't diligent.

Sanford Whiteman

unread,
Jul 15, 2011, 3:55:22 AM7/15/11
to mootool...@googlegroups.com
Well, specifically targeting radios this way will never be possible, because they are just an HTMLInputElement with a specific `type` value (there is no HTMLRadioElement). So there's that.

Even when browsers allow you to extend the subinterfaces of HTMLElement, you are still restricted to the standard subinterfaces. So you can't extend radios or checkboxes separately, and you shouldn't even expect to extend <EM> and <STRONG> or even <SPAN> tags individually, since per the DOM specs, all these hang directly off the generic HTMLElement.

In sum, even in cutting-edge browsers, you may have to check the current element's tag and/or type before continuing, so for simplicity you might as well extend the whole Element. I'd guess that with a _extremely_ high ratio of uninteresting elements to your "interesting" elements, you might actually see that extending, say, just HTMLInputElement would be faster at runtime, but it's unlikely to be worth it.

-- S.

Steve Onnis

unread,
Jul 15, 2011, 4:00:03 AM7/15/11
to mootool...@googlegroups.com
Raio was an example. I know its just an input element which is why i was asking. Something like

$$("input[type=radio]").implement({
style : function () {}
});

Is what i was hoping to be able to do

-----Original Message-----
From: Sanford Whiteman [mailto:sa...@cypressintegrated.com]

Arian Stolwijk

unread,
Jul 15, 2011, 5:46:57 AM7/15/11
to mootool...@googlegroups.com
In MooTools 2 we probably will wrap elements with Class which will give us the possibility to subclass elements.

That way you probably could do something like:

var MyTable = new Class({
     Matches: 'table.foo',
     Extends: Node.Element,
     bar: function(){ ... }
});

Now you can do new MyTable or $$('table.foo') which will return instances of your MyTable class.

Barry van Oudtshoorn

unread,
Jul 15, 2011, 5:55:42 AM7/15/11
to mootool...@googlegroups.com
Arian, I'm struggling to see how using $$ *could* work. What happens if you have two classes that match the same element?

My recommendation would be to extend Elements, so that you could do something along the lines of   | var els= new myElements('.foo'); |   . Each element of MyTables could, then, be wrapped in some arbitrary class of your choosing. You could perhaps even go so far as to do something like   | var els = new MyElements('.foo', MyTable) |   , and have the elements collected pass as a parameter to the MyTable constructor.

Barry van Oudtshoorn
http://barryvan.com.au/
bvanoud...@gmail.com

Ryan Florence

unread,
Jul 16, 2011, 12:14:44 PM7/16/11
to mootool...@googlegroups.com
Here's my solution to custom form elements. Not at all the API you're going for, but thought it might interesting regardless.

Clone it / download it, pop open Demos/index.html

https://github.com/rpflorence/mootools-custom-form


hamburger

unread,
Jul 18, 2011, 6:20:14 AM7/18/11
to MooTools Users
Hello Ryan,
it sounds quite interesting ...

i get the error message offsetParent is null by testing it with moo
1.3.2

Ryan Florence

unread,
Jul 18, 2011, 10:52:59 AM7/18/11
to mootool...@googlegroups.com
Do you have Element.Position from more?

hamburger

unread,
Jul 19, 2011, 1:11:01 PM7/19/11
to MooTools Users
Hello Ryan,
i do have Element.Position
(in my environment i am using the complete framework)

hamburger

unread,
Jul 21, 2011, 10:39:06 AM7/21/11
to MooTools Users
Hello again Ryan,
here is the error-message:
offsetParent is null
parentScroll = offsetParent.getScroll()

i'am using:
mootools-more-1.3.2.1.js and mootools-core-1.3.2-full-nocompat.js
Reply all
Reply to author
Forward
0 new messages