jQuery: The Write Less, Do More JavaScript Library

Plugin Authoring Code Example Incorrect

View: New views
6 Messages — Rating Filter:   Alert me  

Plugin Authoring Code Example Incorrect

by Shane Riley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I created a jQuery plugin for the sake of learning the process and got
it working no problem. In the plugin authoring docs it says that if
you return true inside the each method, you can continue using jQuery
chaining. However the code example above has the return this statement
at the end of the plugin function rather than the each method. While
quickly reading through this article after creating my plugin to
ensure I was following best practices, I placed the return true in the
same spot and my Javascript ceased to run. I tried logging in and
editing the documentation, however it's locked. Just wanted to let
everyone know of the mistake in the hopes someone on here has the
ability to fix it.

Re: Plugin Authoring Code Example Incorrect

by Morten Maxild :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

'return true' is wrong, you need to return the 'wrapped set' to support chaining. The $().each method does return the 'wrapped set' and you can take advantage of this.

HTH
Morten


>-----Original Message-----
>From: Shane Riley [mailto:shanerileydotinfo@...]
>Sent: Saturday, November 07, 2009 1:19 PM
>To: jQuery (English)
>Subject: [jQuery] Plugin Authoring Code Example Incorrect
>
>I created a jQuery plugin for the sake of learning the process and got
>it working no problem. In the plugin authoring docs it says that if
>you return true inside the each method, you can continue using jQuery
>chaining. However the code example above has the return this statement
>at the end of the plugin function rather than the each method. While
>quickly reading through this article after creating my plugin to
>ensure I was following best practices, I placed the return true in the
>same spot and my Javascript ceased to run. I tried logging in and
>editing the documentation, however it's locked. Just wanted to let
>everyone know of the mistake in the hopes someone on here has the
>ability to fix it.


Re: Plugin Authoring Code Example Incorrect

by Shane Riley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I ended up using this syntax instead:
return this.each(function(){});

On Nov 7, 11:43 am, "Morten Maxild" <mmax...@...> wrote:

> 'return true' is wrong, you need to return the 'wrapped set' to support chaining. The $().each method does return the 'wrapped set' and you can take advantage of this.
>
> HTH
> Morten
>
>
>
> >-----Original Message-----
> >From: Shane Riley [mailto:shanerileydoti...@...]
> >Sent: Saturday, November 07, 2009 1:19 PM
> >To: jQuery (English)
> >Subject: [jQuery] Plugin Authoring Code Example Incorrect
>
> >I created a jQuery plugin for the sake of learning the process and got
> >it working no problem. In the plugin authoring docs it says that if
> >you return true inside the each method, you can continue using jQuery
> >chaining. However the code example above has the return this statement
> >at the end of the plugin function rather than the each method. While
> >quickly reading through this article after creating my plugin to
> >ensure I was following best practices, I placed the return true in the
> >same spot and my Javascript ceased to run. I tried logging in and
> >editing the documentation, however it's locked. Just wanted to let
> >everyone know of the mistake in the hopes someone on here has the
> >ability to fix it.

Re: Re: Plugin Authoring Code Example Incorrect

by Morten Maxild :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Exactly...looks correct:-)

>-----Original Message-----
>From: Shane Riley [mailto:shanerileydotinfo@...]
>Sent: Sunday, November 08, 2009 6:44 PM
>To: jQuery (English)
>Subject: [jQuery] Re: Plugin Authoring Code Example Incorrect
>
>I ended up using this syntax instead:
>return this.each(function(){});
>
>On Nov 7, 11:43 am, "Morten Maxild" <mmax...@...> wrote:
>> 'return true' is wrong, you need to return the 'wrapped set' to support
>chaining. The $().each method does return the 'wrapped set' and you can
>take advantage of this.
>>
>> HTH
>> Morten
>>
>>
>>
>> >-----Original Message-----
>> >From: Shane Riley [mailto:shanerileydoti...@...]
>> >Sent: Saturday, November 07, 2009 1:19 PM
>> >To: jQuery (English)
>> >Subject: [jQuery] Plugin Authoring Code Example Incorrect
>>
>> >I created a jQuery plugin for the sake of learning the process and got
>> >it working no problem. In the plugin authoring docs it says that if
>> >you return true inside the each method, you can continue using jQuery
>> >chaining. However the code example above has the return this statement
>> >at the end of the plugin function rather than the each method. While
>> >quickly reading through this article after creating my plugin to
>> >ensure I was following best practices, I placed the return true in the
>> >same spot and my Javascript ceased to run. I tried logging in and
>> >editing the documentation, however it's locked. Just wanted to let
>> >everyone know of the mistake in the hopes someone on here has the
>> >ability to fix it.


Re: Re: Plugin Authoring Code Example Incorrect

by Karl Swedberg-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In the meantime, I changed the wording on that page.

Both of the following will work:
1)
return this.each(function() { /* do something */ });
2)
this.each(function() { /* do something */ });
return this;

--Karl

On Nov 8, 2009, at 12:56 PM, Morten Maxild wrote:

> Exactly...looks correct:-)
>
>> -----Original Message-----
>> From: Shane Riley [mailto:shanerileydotinfo@...]
>> Sent: Sunday, November 08, 2009 6:44 PM
>> To: jQuery (English)
>> Subject: [jQuery] Re: Plugin Authoring Code Example Incorrect
>>
>> I ended up using this syntax instead:
>> return this.each(function(){});
>>
>> On Nov 7, 11:43 am, "Morten Maxild" <mmax...@...> wrote:
>>> 'return true' is wrong, you need to return the 'wrapped set' to  
>>> support
>> chaining. The $().each method does return the 'wrapped set' and you  
>> can
>> take advantage of this.
>>>
>>> HTH
>>> Morten
>>>
>>>
>>>
>>>> -----Original Message-----
>>>> From: Shane Riley [mailto:shanerileydoti...@...]
>>>> Sent: Saturday, November 07, 2009 1:19 PM
>>>> To: jQuery (English)
>>>> Subject: [jQuery] Plugin Authoring Code Example Incorrect
>>>
>>>> I created a jQuery plugin for the sake of learning the process  
>>>> and got
>>>> it working no problem. In the plugin authoring docs it says that if
>>>> you return true inside the each method, you can continue using  
>>>> jQuery
>>>> chaining. However the code example above has the return this  
>>>> statement
>>>> at the end of the plugin function rather than the each method.  
>>>> While
>>>> quickly reading through this article after creating my plugin to
>>>> ensure I was following best practices, I placed the return true  
>>>> in the
>>>> same spot and my Javascript ceased to run. I tried logging in and
>>>> editing the documentation, however it's locked. Just wanted to let
>>>> everyone know of the mistake in the hopes someone on here has the
>>>> ability to fix it.
>


Re: Re: Plugin Authoring Code Example Incorrect

by Shane Riley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Karl. Turns out both worked for me, but I had placed the return  
just outside the plugin function instead of within it.


On Nov 8, 2009, at 1:13 PM, Karl Swedberg wrote:

> In the meantime, I changed the wording on that page.
>
> Both of the following will work:
> 1)
> return this.each(function() { /* do something */ });
> 2)
> this.each(function() { /* do something */ });
> return this;
>
> --Karl
>
> On Nov 8, 2009, at 12:56 PM, Morten Maxild wrote:
>
>> Exactly...looks correct:-)
>>
>>> -----Original Message-----
>>> From: Shane Riley [mailto:shanerileydotinfo@...]
>>> Sent: Sunday, November 08, 2009 6:44 PM
>>> To: jQuery (English)
>>> Subject: [jQuery] Re: Plugin Authoring Code Example Incorrect
>>>
>>> I ended up using this syntax instead:
>>> return this.each(function(){});
>>>
>>> On Nov 7, 11:43 am, "Morten Maxild" <mmax...@...> wrote:
>>>> 'return true' is wrong, you need to return the 'wrapped set' to  
>>>> support
>>> chaining. The $().each method does return the 'wrapped set' and  
>>> you can
>>> take advantage of this.
>>>>
>>>> HTH
>>>> Morten
>>>>
>>>>
>>>>
>>>>> -----Original Message-----
>>>>> From: Shane Riley [mailto:shanerileydoti...@...]
>>>>> Sent: Saturday, November 07, 2009 1:19 PM
>>>>> To: jQuery (English)
>>>>> Subject: [jQuery] Plugin Authoring Code Example Incorrect
>>>>
>>>>> I created a jQuery plugin for the sake of learning the process  
>>>>> and got
>>>>> it working no problem. In the plugin authoring docs it says that  
>>>>> if
>>>>> you return true inside the each method, you can continue using  
>>>>> jQuery
>>>>> chaining. However the code example above has the return this  
>>>>> statement
>>>>> at the end of the plugin function rather than the each method.  
>>>>> While
>>>>> quickly reading through this article after creating my plugin to
>>>>> ensure I was following best practices, I placed the return true  
>>>>> in the
>>>>> same spot and my Javascript ceased to run. I tried logging in and
>>>>> editing the documentation, however it's locked. Just wanted to let
>>>>> everyone know of the mistake in the hopes someone on here has the
>>>>> ability to fix it.
>>
>