Skip to content

misleading example about retrieval of widget instance through data() #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
teo1978 opened this issue Jun 22, 2014 · 1 comment
Closed

Comments

@teo1978
Copy link
Contributor

teo1978 commented Jun 22, 2014

http://learn.jquery.com/plugins/stateful-plugins-with-widget-factory/#the-widget-factory-under-the-hood

When a plugin instance is created, it is stored on the original DOM element using jQuery.data, with the plugin name as the key

That vaguely suggests that the plugin name is used as the key, and one may wonder whether the namespace is included, and how it is joined to the plugin name

The code example seems to clear any possible dubt:

var bar = $( "<div />")
    .appendTo( "body" )
    .progressbar()
    .data( "progressbar" );

The namespace is omitted. And that's wrong. That will never work.

The example was so clear and unambiguous (but wrong) that it led me to think that there was an actual bug in the software, which I wrongly reported.
http://bugs.jqueryui.com/ticket/10123#comment:2

The namespace is infact needed, and it must be joined to the plugin name with a hyphen.

That should be stated explicitely, and the example code fixed:

var bar = $( "<div />")
    .appendTo( "body" )
    .progressbar()
    .data( "nmk-progressbar" );
arthurvr pushed a commit to arthurvr/learn.jquery.com that referenced this issue Jan 4, 2015
The part about retrieving the plugin instance from the dom element using .data() was vague about the key used: it said "the plugin name" without mentioning that the full name is needed, including the namespace and a hyphen as a separator. The code example was plain wrong, using only the bare plugin name, which would not work.

Fixes jquery#515
Closes jquery#516
@jerrylammel
Copy link

jerrylammel commented Jul 21, 2020

I think the example given is still misleading even with the namespace clarified, i.e. nmk-progressbar.

The issue is that .progressbar() actually creates an instance of the Progressbar Widget of jQuery UI (https://api.jqueryui.com/progressbar/#quick-nav ) rather than one of the nmk-progressbar built
with Widget Factory.

The code snippet happens to make it being able to access the plugin instance of nmk-progressbar
by using the key "nmk-progressbar", which does run successfully but incorrect in meaning.
var bar = $( "<div />") .appendTo( "body" ) .progressbar() .data( "nmk-progressbar" );

As considering the context in the article is with jQuery UI Widget Factory, this suggests audiences
that it is a way, using .progressbar(), to make an instance of the plugin nmk-progressbar.

I hope I am not wrong and expect for comments from anyone experienced.
Thanks ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants