Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Access data of an appended element #470

Closed
wants to merge 6 commits into from

2 participants

@remichaignon

When appending an element with data attributes, we couldn't grab those attributes by calling .data() on the appended element. So we added that functionality (as we work with templates, this functionality was required).
Added a test called testGettingDataAfterAppend to validate the code.

Plus this fixes the issue in testAllData where the data-mode of data_full would be undefined. Why was there an assert on it being undefined? Was that the desired behavior? I changed that assert.

@mislav mislav was assigned
@mislav mislav closed this in 4cf3020
@mislav
Collaborator

Your issue had nothing to do with appending; you just expected the "data-*" attributes to pre-populate the object returned by data(), and that's the behavior that's present in jQuery. I've now implemented this behavior, take a look.

Thanks for reporting!

@lopper lopper referenced this pull request from a commit in buddydvd/zepto
@mislav mislav data module: have initial data hold "data-*" attribute values
closes #470
828f26d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 12, 2012
  1. @remichaignon

    Add full data functionalities - .data() returns the full object (from…

    remichaignon authored
    … HTML tag as well) + .data({...}) will set multiple data for that node
Commits on Mar 27, 2012
  1. @remichaignon
  2. @remichaignon
  3. @remichaignon
Commits on Apr 6, 2012
  1. @remichaignon

    Merge with latest zepto

    remichaignon authored
  2. @remichaignon

    Switch to single quote

    remichaignon authored
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 2 deletions.
  1. +7 −0 src/data.js
  2. +11 −2 test/data.html
View
7 src/data.js
@@ -19,6 +19,13 @@
var id = node[exp] || (node[exp] = ++uuid),
store = data[id] || (data[id] = {})
if (name !== undefined) store[name] = value
+ else {
+ var i, l, attributes = node.attributes;
+ for (i = 0, l = attributes.length; i < l; i++) {
+ name = attributes[i].name
+ if ( name.indexOf('data-') === 0 ) store[name.substring(5)] = attributes[i].value
+ }
+ }
return store
}
View
13 test/data.html
@@ -19,6 +19,7 @@
<div id="data_attr" data-one="uno" data-two="due"></div>
<div id="data_full" data-mode="awesome"></div>
<div id="data_obj" data-mode="awesome"></div>
+ <div id="data_append"></div>
<ol id="data_list">
<li data-category="arts"></li>
<li data-category="science"></li>
@@ -103,7 +104,7 @@
t.assertEqual('ichi', all.one)
t.assertEqual('ni', all.two)
t.assertEqual('Kurosawa', all.person.name)
- t.assertUndefined(all.mode)
+ t.assertEqual('awesome', all.mode)
},
testSettingDataWithObj: function(t){
@@ -141,8 +142,16 @@
var values2 = items.map(function(){ return $(this).data('answer') })
t.assertEqual('42, 42', values2.join(', '))
- }
+ },
+
+ testGettingDataAfterAppend: function(t){
+ var el = $('#data_append')
+ el.append('<div id="data_appendix" data-mode="awesome"></div>')
+
+ var all = $('#data_appendix').data()
+ t.assertEqual(all.mode, 'awesome')
+ }
})
</script>
</body>
Something went wrong with that request. Please try again.