jquery / jquery/learn.jquery.com

misleading example on access of widget instance through .data()

Open
#796 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Chapter: plugins
Dominant language
JavaScript
Stars
915
Forks
471
PR merge metrics
No merged PRs in 30d

Description

https://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's full name (the plugin's namespace, plus a hyphen, plus the plugin's name) as the key.

var bar = $( "<div />")
    .appendTo( "body" )
    .progressbar()
    .data( "nmk-progressbar" );
 
// Call a method directly on the plugin instance.
bar.option( "value", 50 );
 
// Access properties on the plugin instance.
alert( bar.options.value );

I think the example given is misleading even with the namespace specified in accessing jQuery.data,
i.e. .data("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 plugin 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.

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 ^_^

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the linked jQuery Learning Center page and inspect the “The Widget Factory Under the Hood” section, especially the .progressbar() and .data("nmk-progressbar") example. Update the example or its explanation so it accurately distinguishes the jQuery UI progressbar instance from the nmk-progressbar plugin, then verify the rendered documentation is clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, jquery
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.