Skip to content

Commit

Permalink
add dom.data()
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Apr 12, 2015
1 parent af62a12 commit b95f29f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lib/dom.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ class Wrapper
classes: ->
return @node.className.split(/\s+/)

data: (key, value) ->
if value?
@node['ql-data'] = {} unless @node['ql-data']?
@node['ql-data'][key] = value
return this
else
return @node['ql-data']?[key]

descendants: ->
return _.map(@node.getElementsByTagName('*'))

Expand Down
31 changes: 31 additions & 0 deletions test/unit/lib/dom.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,37 @@ describe('DOM', ->
)
)

describe('data', ->
beforeEach( ->
@container.id = 'test-data'
)

it('get nonexistent value', ->
data = dom(@container).data('nonexistent')
expect(data).toBeUndefined()
)

it('get/set value', ->
dom(@container).data('a', 1).data('b', 2)
elem = document.getElementById('test-data')
expect(dom(elem).data('a')).toEqual(1)
expect(dom(elem).data('b')).toEqual(2)
)

it('overwrite value', ->
dom(@container).data('a', 1).data('a', 2)
elem = document.getElementById('test-data')
expect(dom(elem).data('a')).toEqual(2)
)

it('removed node', ->
dom(@container).data('a', 1)
elem = document.getElementById('test-data')
$(@container).remove()
expect(dom(elem).data('a')).toEqual(1)
)
)

describe('events', ->
describe('on()', ->
beforeEach( ->
Expand Down

0 comments on commit b95f29f

Please sign in to comment.