On Dec 14, 11:53 pm, David_ca <david.calling...@gmail.com> wrote:
> For me it is more natural to do the same thing using the below syntax
> but it does not work. Can someone tell me why it doesn't work and/or
> show
> my the best practice approach to create a variable of a widget object
> instantiation and reference it
> later.
>
> $().ready(function() {
>         var aGreen = $('#test4 .target').green4();
>         aGreen.darker();
>
> });

$('#test4 .target').green4() returns a jQuery object. From that
object, you can get to the actual plugin instance using the .data()
method:

var aGreen = $('#test4 .target').green4().data('green4);
aGreen.darker();

All plugin instances are stored using .data() with the name of the
plugin as the key.


> ///////////////////////////////////////////////////////////////////
> Question 2) How make a function return something.
> Supposing my widget has a getHello() method that returns a "hello"
> string. How do I call that method of my widget. I tried the below code
> but it
> does not work.
> $().ready(function() {
>         var aGreen = $('#test4 .target').green4();
>         console.log("response: "+ $('#test4 .target').green4
> ('getHello'));
>
> });

This works as of 1.8a1. Returning any value besides the plugin
instance or undefined, will now act as a getter. Returning the plugin
instance or undefined (or no return value) will make the plugin call
chainable. With 1.7.x, you can make a method that is a getter by
doing:

$.ui.green4.getter = 'getHello';

The getter property is a space delimited list of methods that will act
as getters. Note that this does not allow a single method to both be a
getter and setter because it will never return the original jQuery
object.


> ///////////////////////////////////////////////////////////////////
> Question 3) How to pass a parameter to a function.
> Supposing I have setDarker(value) function that takes a parameter
> How do I invoke that function. Below is the syntax I hope would
> work but it doesn't.
>
> $().ready(function() {
>         var aGreen = $('#test4 .target').green4();
>         aGreen.setDarker("big");
>
> });

Two options:
1) jQuery method: Use the jQuery object and pass the parameters as
additional parameters to the plugin method.

aGreen.green4('setDarker', 'big');

2) plugin instance: Get the plugin instance as shown above from
question #1 and use the standard function syntax.

aGreenInstance.setDarker('big');

--

You received this message because you are subscribed to the Google Groups 
"jQuery UI" group.
To post to this group, send email to jquery...@googlegroups.com.
To unsubscribe from this group, send email to 
jquery-ui+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/jquery-ui?hl=en.


Reply via email to