plugin options that take an element as a value

17 views
Skip to first unread message

Scott González

unread,
Nov 12, 2009, 9:46:59 PM11/12/09
to jQuery UI Development
We've got a lot of plugins that take an element as a option's value (position's of, draggable's helper, etc.). Each of these work slightly differently because there are so many cases to handle that some of them get overlooked. I wrote a function a long time ago to handle this, but never ended up using it anywhere. Here are the cases I came up with back then as possible options:

Note: In many cases there is a context for determining the element. For example, draggable's helper has a context of the actual draggable element.

jQuery object - just return the object again
DOMElement - wrap it in a jQuery object
array of DOMElements - wrap them in a jQuery object
selector - return the jQuery object that matches the selection
the string "parent" - return the parent of the context element
the string "clone" - return a clone of the context element
function - return the result of executing the function and passing the context element as a parameter
anything else - return an empty jQuery object

I'd like to go through and test this on all of our plugins, but I wanted to see if anything had any other cases that we should be handling.

Here's the code: http://gist.github.com/233531

Paul Bakaus

unread,
Nov 13, 2009, 2:22:50 AM11/13/09
to jquery...@googlegroups.com
Good idea, code looks nifty as well :)

Yeah, let's get this into the widget factory or core, it's tiny and important enough to justify.

2009/11/13 Scott González <scott.g...@gmail.com>

Jörn Zaefferer

unread,
Nov 13, 2009, 2:52:12 AM11/13/09
to jquery...@googlegroups.com
Would this work?

.positionTo({
 my: ...,
 at: ..,
 of: "parent"
})

Jörn

2009/11/13 Scott González <scott.g...@gmail.com>

Richard D. Worth

unread,
Nov 13, 2009, 7:44:07 AM11/13/09
to jquery...@googlegroups.com
the string "self" - return the context element ? Or maybe "this" ?

- Richard

2009/11/12 Scott González <scott.g...@gmail.com>

Scott González

unread,
Nov 13, 2009, 8:05:08 AM11/13/09
to jquery...@googlegroups.com
On Fri, Nov 13, 2009 at 2:52 AM, Jörn Zaefferer <joern.z...@googlemail.com> wrote:
Would this work?

.positionTo({
 my: ...,
 at: ..,
 of: "parent"
})

I don't see why not. It currently doesn't, but that's the point of this thread :-)

Scott González

unread,
Nov 13, 2009, 8:09:20 AM11/13/09
to jquery...@googlegroups.com
On Fri, Nov 13, 2009 at 7:44 AM, Richard D. Worth <rdw...@gmail.com> wrote:
the string "self" - return the context element ? Or maybe "this" ?

I think everywhere that we would currently do this we just use null for the option, e.g., draggable's helper. I'm not sure if that means we should return the context element as a fallback instead of an empty jQuery object or if we should add "self" as an option and make that the default for draggable's helper option. A third option would be to have the plugin check if a value even exists:

var helper = options.helper ? element(options.helper) : this.element;

I'd prefer not having to do that check, so I think one of the first two options is better.

Jörn Zaefferer

unread,
Nov 13, 2009, 8:15:13 AM11/13/09
to jquery...@googlegroups.com
Ok, one more: Where would you put that method and how to you call it? On the widget prototype? A static method in the $.ui namespace?

Jörn

2009/11/13 Scott González <scott.g...@gmail.com>
On Fri, Nov 13, 2009 at 2:52 AM, Jörn Zaefferer <joern.z...@googlemail.com> wrote:

Scott González

unread,
Nov 13, 2009, 8:31:03 AM11/13/09
to jquery...@googlegroups.com
On Fri, Nov 13, 2009 at 8:15 AM, Jörn Zaefferer <joern.z...@googlemail.com> wrote:
Ok, one more: Where would you put that method and how to you call it? On the widget prototype? A static method in the $.ui namespace?

I'd want to put it in the $.ui namespace because it is useful outside of widgets. I'm not sure what to name it though.

Usage: var helper = $.ui.element(this.options.helper, this.element);
Reply all
Reply to author
Forward
0 new messages