-
Notifications
You must be signed in to change notification settings - Fork 639
Added html5data methods set/get #91
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
037bc71
9b03ceb
7ec37f8
9320c59
02333f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3103,12 +3103,94 @@ | |
|
||
return value; | ||
} | ||
|
||
|
||
//simple test for native HTML5 dataset existence | ||
var NATIVEHTML5DATASET = (function (){ | ||
var testelement = new Element("div",{"data-test-this-thing":"test"}); | ||
if(typeof testelement.dataset != 'undefined' && typeof testelement.dataset.testThisThing != 'undefined') return true; | ||
else return false; | ||
} | ||
/** | ||
* Element.gethtml5data(@element[, datakey]) -> Object | ||
* | ||
* Retrieves HTML5 data-* attributes set on `element`. | ||
* | ||
* This will clear up any problems retreiving these data attributes on browsers | ||
* that don't support them. | ||
* | ||
* ##### Example | ||
* | ||
* language: html | ||
* <div id="dataitem" data-old-url="http://prototypejs.org"></div> | ||
* | ||
* Then: | ||
* $("dataitem").gethtml5data().oldUrl; | ||
* // -> "http://prototypejs.org" | ||
**/ | ||
function gethtml5data(element,datalabel){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for 1 - thats a good idea - the other code changes were simple ones - let me work on separating them out for 2 I was thinking of doing that - but I wanted to be consistent with the return value (an object) vs "if you ask for one value you'll get a string - otherwise you get an object" what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of time similar methods in other frameworks are used to retrieve exactly one value. Compare
with
Even if you need two or three values from the same element
looks not worser than
Also in this example count the time needed for enumerating all attributes of an element and retrieving all P.S. Everything is an object :) |
||
if(!(element = $(element))) return; | ||
var returnobject = {}; | ||
if(NATIVEHTML5DATASET) | ||
{ | ||
if(datalabel != undefined) returnobject[datalabel.camelize()] = element.dataset[datalabel]; | ||
else returnobject = element.dataset; | ||
} | ||
else | ||
{ | ||
if(datalabel != undefined) returnobject[datalabel.camelize()] = element.readAttribute('data-'+datalabel); | ||
else | ||
{ | ||
var label = ""; | ||
var numberattributes = element.attributes.length; | ||
for(var t = 0; t < numberattributes ; t++) | ||
{ | ||
if(element.attributes[t].name.match(/^data-.+/)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for regexp. |
||
{ | ||
label = element.attributes[t].name.replace(/^data-/,'').camelize(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, just use substring rather than |
||
returnobject[label] = element.attributes[t].value; | ||
} | ||
} | ||
} | ||
} | ||
return returnobject; | ||
} | ||
/** | ||
* Element.sethtml5data(@element,datakey [,value]) -> Object | ||
* | ||
* Sets/Removes HTML5 data-* attributes on `element`. If `value` is not defined or null it will clear the data value on the element. | ||
* | ||
* This will clear up any problems setting these data attributes on browsers | ||
* that don't support them. | ||
* | ||
* ##### Example | ||
* | ||
* language: html | ||
* <div id="dataitem" data-old-url="http://prototypejs.org"></div> | ||
* | ||
* Then: | ||
* $("dataitem").sethtml5data('newUrl','http://api.prototypejs.org'); | ||
* $("dataitem").sethtml5data('oldUrl',null); | ||
* | ||
* language: html | ||
* <div id="dataitem" data-new-url="http://api.prototypejs.org"></div> | ||
**/ | ||
function sethtml5data(element,datalabel,value){ | ||
if(typeof value != undefined) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't agree with this change as function isUndefined(object) {
return typeof object === "undefined";
} granted the line should be corrected to if(typeof value !== 'undefined') |
||
{ | ||
if(NATIVEHTML5DATASET) element.dataset[datalabel.camelize()] = value; | ||
element.writeAttribute("data-"+datalabel.underscore().dasherize(),value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 3177, 3180 and 3185 can be merged for
|
||
} | ||
else | ||
{ | ||
delete element.dataset[datalabel.camelize()]; | ||
element.writeAttribute("data-"+datalabel.underscore().dasherize(),null); | ||
} | ||
} | ||
Object.extend(methods, { | ||
getStorage: getStorage, | ||
store: store, | ||
retrieve: retrieve | ||
retrieve: retrieve, | ||
gethtml5data: gethtml5data, | ||
sethtml5data: sethtml5data | ||
}); | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return (testelement.dataset && testelement.dataset.testThisThing === 'test');