Skip to content

Commit

Permalink
Element#data() should return nil for undefined attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Feb 7, 2014
1 parent ce26192 commit 1814202
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,9 @@

* Add `LocalStorage` implementation.

* Fix Element#data() to return nil for an undefined data attribute instead
of null.

## 0.1.2 2013-12-01

* Support setting html content through `Element#html()`.
Expand Down
9 changes: 8 additions & 1 deletion opal/opal-jquery/element.rb
Expand Up @@ -59,7 +59,7 @@ def self.expose(*methods)
# Bridged functions - we just expose all core jquery functions as ruby
# methods on this class.
expose :after, :before, :parent, :parents, :prepend, :prev, :remove
expose :hide, :show, :toggle, :children, :blur, :closest, :data
expose :hide, :show, :toggle, :children, :blur, :closest
expose :focus, :find, :next, :siblings, :text, :trigger, :append
expose :height, :width, :serialize, :is, :filter, :last, :first
expose :wrap, :stop, :clone, :empty
Expand Down Expand Up @@ -182,6 +182,13 @@ def animate(params, &block)
}
end

def data(*args)
%x{
var result = self.data.apply(self, args);
return result == null ? nil : result;
}
end

# Start a visual effect (e.g. fadeIn, fadeOut, …) passing its name.
# Underscored style is automatically converted (e.g. `effect(:fade_in)`).
# Also accepts additional arguments and a block for the finished callback.
Expand Down
22 changes: 22 additions & 0 deletions spec/element_spec.rb
Expand Up @@ -183,6 +183,28 @@
foo.class_name.should == 'bar'
end
end

end

describe "Element#data" do
html <<-HTML
<div id="data-foo"></div>
<div id="data-ford" data-authur="dent"></div>
HTML

it "sets a data attribute" do
foo = Element.id('data-foo')
foo.data 'bar', 'baz'
expect(foo.data('bar')).to eq('baz')
end

it "can retrieve a data attribute" do
expect(Element.id('data-ford').data('authur')).to eq('dent')
end

it "returns nil for an undefined data attribute" do
expect(Element.id('data-ford').data('not-here')).to be_nil
end
end

describe "Element#html" do
Expand Down

0 comments on commit 1814202

Please sign in to comment.