Class: Browser::DOM::Element::Data

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
opal/browser/dom/element/data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Data

Returns a new instance of Data.



6
7
8
9
10
11
12
13
# File 'opal/browser/dom/element/data.rb', line 6

def initialize(element)
  @element = element
  @native  = element.to_n

  unless defined?(`#@native.$data`)
    `#@native.$data = {}`
  end
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



4
5
6
# File 'opal/browser/dom/element/data.rb', line 4

def element
  @element
end

Instance Method Details

#[](name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'opal/browser/dom/element/data.rb', line 45

def [](name)
  if data = @element["data-#{name}"]
    return data
  end

  %x{
    var value = #@native.$data[name];

    if (value === undefined) {
      return nil;
    }
    else {
      return value;
    }
  }
end

#[]=(name, value) ⇒ Object



62
63
64
# File 'opal/browser/dom/element/data.rb', line 62

def []=(name, value)
  `#@native.$data[name] = value`
end

#assign(data) ⇒ Object



37
38
39
40
41
42
43
# File 'opal/browser/dom/element/data.rb', line 37

def assign(data)
  data.each {|name, value|
    `#@native.$data[name] = value`
  }

  self
end

#each(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'opal/browser/dom/element/data.rb', line 17

def each(&block)
  return enum_for :each unless block

  %x{
    var data = #@native.$data;

    for (var key in data) {
      #{block.call `key`, `data[key]`};
    }
  }

  @element.attributes.each {|name, value|
    if name =~ /^data-(.*)$/
      block.call $1, value
    end
  }

  self
end