开发者

Conflicts between existing/new data keys?

Attaching data to开发者_开发百科 elements in jQuery is easy:

$(selector).data(key, value).

However, it occurred to me that I may want to prefix all my keys with my root namespace. For example:

$(selector).data("MyDotComApp." + key, value)

Is this a waste of time? Am I overthinking this? Do I need to worry about overwriting any data the core framework writes, or plugins?

Is there a best practice for naming data keys?


Certainly wouldn't hurt, although you wouldn't necessarily need to do it via concatenation of all the various keys.

Whenever you want to create data for your specific domain, you could just create one key in data on the element with the name of your domain, and give it an object that can store the individual keys.

Example: http://jsfiddle.net/XwJbQ/

  // The myDomain key in data references its own object
$('img').data('myDomain', {});

  // then individual keys are added to the object myDomain references
$('img').data('myDomain').someKey = "some value";
$('img').data('myDomain').someOtherKey = "some other value";

  // and are retrieved the same way
alert( $('img').data('myDomain').someKey );
alert( $('img').data('myDomain').someOtherKey );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜