Permalink
Browse files

support number, boolean, JSON types in data attributes

  • Loading branch information...
1 parent 130cb93 commit d9f9319e339a811ec5514d83daf912a800cbe269 @mislav mislav committed Oct 6, 2012
Showing with 55 additions and 3 deletions.
  1. +2 −1 src/data.js
  2. +25 −1 src/zepto.js
  3. +3 −1 test/data.html
  4. +25 −0 test/zepto.html
View
@@ -38,7 +38,8 @@
var store = {}
$.each(node.attributes, function(i, attr){
if (attr.name.indexOf('data-') == 0)
- store[camelize(attr.name.replace('data-', ''))] = attr.value
+ store[camelize(attr.name.replace('data-', ''))] =
+ $.zepto.deserializeValue(attr.value)
})
return store
}
View
@@ -235,6 +235,29 @@ var Zepto = (function() {
svg ? (klass.baseVal = value) : (node.className = value)
}
+ // "true" => true
+ // "false" => false
+ // "null" => null
+ // "42" => 42
+ // "42.5" => 42.5
+ // JSON => parse if valid
+ // String => self
+ function deserializeValue(value) {
+ var num
+ try {
+ return value ?
+ value == "true" ||
+ ( value == "false" ? false :
+ value == "null" ? null :
+ !isNaN(num = Number(value)) ? num :
+ /^[\[\{]/.test(value) ? $.parseJSON(value) :
+ value )
+ : value
+ } catch(e) {
+ return value
+ }
+ }
+
$.isFunction = isFunction
$.isObject = isObject
$.isArray = isArray
@@ -509,7 +532,7 @@ var Zepto = (function() {
},
data: function(name, value){
var data = this.attr('data-' + dasherize(name), value)
- return data !== null ? data : undefined
+ return data !== null ? deserializeValue(data) : undefined
},
val: function(value){
return (value === undefined) ?
@@ -659,6 +682,7 @@ var Zepto = (function() {
// Export internal API functions in the `$.zepto` namespace
zepto.camelize = camelize
zepto.uniq = uniq
+ zepto.deserializeValue = deserializeValue
$.zepto = zepto
return $
View
@@ -120,12 +120,14 @@
},
testInitialDataFromAttributes: function(t){
- var el = $('<div data-foo=bar data-foo-bar=baz />'),
+ var el = $('<div data-foo=bar data-foo-bar=baz data-empty data-num=42 />'),
store = el.data()
t.assertEqual('bar', store.foo)
t.assertEqual('baz', store.fooBar)
t.assertUndefined(store['foo-bar'])
+ t.assertIdentical('', store.empty)
+ t.assertIdentical(42, store.num)
},
testGettingBlanks: function(t){
View
@@ -1549,6 +1549,31 @@
t.assertUndefined(el.data('does-not-exist'))
},
+ testDataNumberType: function(t){
+ var el = $('<div data-num=42 />')
+ t.assertIdentical(42, el.data('num'))
+ },
+
+ testDataBooleanType: function(t){
+ var el = $('<div data-true=true data-false=false />')
+ t.assertTrue(el.data('true'))
+ t.assertFalse(el.data('false'))
+ },
+
+ testDataNullType: function(t){
+ var el = $('<div data-nil=null />')
+ t.assertNull(el.data('nil'))
+ },
+
+ testDataJsonType: function(t){
+ var el = $('<div data-json=\'["one", "two"]\' data-invalid=\'[boom]\' />')
+ var json = el.data('json')
+ t.assertEqual(2, json.length)
+ t.assertEqual("one", json[0])
+ t.assertEqual("two", json[1])
+ t.assertEqual('[boom]', el.data('invalid'))
+ },
+
testVal: function(t) {
var input = $('#attr_val')

0 comments on commit d9f9319

Please sign in to comment.