lib/opal/jquery/element.rb in opal-jquery-0.3.0 vs lib/opal/jquery/element.rb in opal-jquery-0.4.0

- old
+ new

@@ -242,10 +242,15 @@ # @!method append(content) # # @param content [String, Element] alias_native :append + # @!method prepend(content) + # + # @param content [String, Element] + alias_native :prepend + # @!method serialize alias_native :serialize # @!method is(selector) # @return [true, false] @@ -294,17 +299,10 @@ alias_native :prop alias succ next alias << append - # @!method []=(attr, value) - # - # Set the given attribute `attr` on each element in this collection. - # - # @see http://api.jquery.com/attr/ - alias_native :[]=, :attr - # @!method add_class(class_name) alias_native :add_class, :addClass # @!method append_to(element) alias_native :append_to, :appendTo @@ -318,16 +316,25 @@ # content. Content can either be a string or another {Element}. # # @param content [String, Element] alias_native :html=, :html + # @!method index(selector_or_element = nil) + alias_native :index + + # @!method is?(selector) + alias_native :is?, :is + # @!method remove_attr(attr) alias_native :remove_attr, :removeAttr # @!method remove_class(class_name) alias_native :remove_class, :removeClass + # @!method submit() + alias_native :submit + # @!method text=(text) # # Set text content of each element in this collection. # # @see #text @@ -382,23 +389,43 @@ def to_n self end def [](name) - `self.attr(name) || nil` + %x{ + var value = self.attr(name); + if(value === undefined) return nil; + return value; + } end - def attr(name, value=nil) - if value.nil? - `self.attr(name) || nil` - else - `self.attr(name, value)` - end + # Set the given attribute `attr` on each element in this collection. + # + # @see http://api.jquery.com/attr/ + def []=(name, value) + `return self.removeAttr(name)` if value.nil? + `self.attr(name, value)` end + def attr(*args) + %x{ + var size = args.length; + switch (size) { + case 1: + return #{self[`args[0]`]}; + break; + case 2: + return #{self[`args[0]`] = `args[1]`}; + break; + default: + #{raise ArgumentError, '#attr only accepts 1 or 2 arguments'} + } + } + end + def has_attribute?(name) - `!!self.attr(name)` + `self.attr(name) !== undefined` end def append_to_body `self.appendTo(document.body)` end @@ -633,10 +660,33 @@ } block end + def one(name, sel = nil, &block) + %x{ + var wrapper = function(evt) { + if (evt.preventDefault) { + evt = #{Event.new `evt`}; + } + + return block.apply(null, arguments); + }; + + block._jq_wrap = wrapper; + + if (sel == nil) { + self.one(name, wrapper); + } + else { + self.one(name, sel, wrapper); + } + } + + block + end + def off(name, sel, block = nil) %x{ if (sel == null) { return self.off(name); } @@ -645,9 +695,14 @@ } else { return self.off(name, sel, block._jq_wrap); } } + end + + # returns an Array of Hashes + def serialize_array + `self.serializeArray()`.map { |e| Hash.new(e) } end alias size length def value