Proper use of "ns"

Proper use of "ns"

I'd like to hear some opinions on the proper use of jQuery Mobile ns option and jqmData() function and :jqmData selector.

As I understand it, if you set the global option ns, then whatever string you put there will be required to be prepended to all data-attributes.

So, if you set ns to foo-, then, instead of data-role="page", you will have to write data-foo-role="page".

The idea behind this is that it can get you out of situations where the JQM data-attributes conflict with those of some other library you are using.

The question is, where you can use this and where you cannot.

As a plugin author, I think I need to make sure that my plugin plays along with this. But in what context? Only when dealing with widget initialization and options that can be specified in HTML? What about internal data that is stored in the DOM? (e.g. there is no data- attribute in the HTML - only data stored there by the widget.)

When a plugin stores data in the DOM,

- Should it use jqmData()?

- Or should it use .data(), possibly with it's own namespacing? So, for example, should jquery.mobile.iscrollview use .data() with it's own namespace of, perhaps, iscroll-, or follow the JQM convention of no namespace but with an option, and in either case, provide the ability to globally set a namespace for the widget?

What about applications? If your application stores data in the DOM, should you be using jqmData(), or just .data(), again, with the possibility of your own namespacing?

I think the latter is fairly clear. Applications should use their own namespace for data items that they uniquely store. You have no way of knowing what new data- attributes JQM will use in the future. So, you should prefix some string to insure that there can never be a conflict, even if you don't have some conflict between JQM and some other library. So, you probably should consistently use a little helper function like myAppData() in code, and use your namespace in any data- attributes.

On the other hand, if your application references standard jQuery Mobile data- attributes (as data, not as attributes) then you should always use jqmData(), in case you might someday make us of the global ns option.

The reason I ask is that the method for getting a real reference to a widget function has changed in jQuery Mobile 1.3 widget factory.

It used to work like this:

var iscrollview = $page.find(".iscroll-wrapper").jqmData("iscrollview");

But, now with 1.3, it works like this:

var iscrollview = $page.find(".iscroll-wrapper").jqmData("mobileIscrollview");

So, the widget factory has now added a mobile prefix to the key, and capitalized the widget name.

(I'd thought at first that jqmData() wasn't usable for this any more. I was wrong. I just didn't know to look for the right key. Note that jqmData() is missing one functionality of .data() - you cannot call it without parameters to retrieve all of the data for a node.)

Now, if you use ns, setting it, say, it "jqm" this becomes, for example:

  jqmData("jqm-mobileIscrollview"), which seems redundant.