Class: KnockoutArray

Inherits:
Array
  • Object
show all
Defined in:
lib/polished/knockout/knockout_array.rb

Overview

This class is used internally and you shouldn’t need to initialize it yourself or worry too much whether an array is a standard array or a KnockoutArray

It’s essentially a wrapper around KO’s observableArray knockoutjs.com/documentation/observableArrays.html

Instance Method Summary collapse

Instance Method Details

#<<(obj) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/polished/knockout/knockout_array.rb', line 53

def <<(obj)
  obj = collection_check(obj)
  
  ret = super(obj)
  
  `self.ko_observable.push(obj.$to_n())`
  
  ret
end

#clearObject



105
106
107
108
109
110
111
# File 'lib/polished/knockout/knockout_array.rb', line 105

def clear()
  ret = super
  
  `self.ko_observable.removeAll()`
  
  ret
end

#collection_check(obj) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/polished/knockout/knockout_array.rb', line 31

def collection_check(obj)
  if @collection_class_name and obj.is_a?(Hash)
    Kernel.const_get(@collection_class_name).new_via_collection(obj, @collection_parent)
  else
    obj
  end
end

#concat(other) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/polished/knockout/knockout_array.rb', line 39

def concat(other)
  if Array === other
    other = other.to_a
  else
    other = Opal.coerce_to(other, Array, :to_ary).to_a
  end

  other.each do |item|
    self.send('<<', item)
  end

  self
end

#delete(obj) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/polished/knockout/knockout_array.rb', line 93

def delete(obj)
  obj_index = index(obj)
  ret = nil
  
  unless obj_index == nil
    ret = super(obj)
    `self.ko_observable.splice(obj_index, 1)`
  end
  
  ret
end

#delete_at(index) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/polished/knockout/knockout_array.rb', line 83

def delete_at(index)
  ret = super(index)
  
  unless ret == nil
    `self.ko_observable.splice(index, 1)`
  end
  
  ret
end

#serialize_js_dataObject



22
23
24
# File 'lib/polished/knockout/knockout_array.rb', line 22

def serialize_js_data
  `ko.toJS(#{@ko_observable})`
end

#set_collection_class(class_name, collection_parent) ⇒ Object



26
27
28
29
# File 'lib/polished/knockout/knockout_array.rb', line 26

def set_collection_class(class_name, collection_parent)
  @collection_class_name = class_name if class_name
  @collection_parent = collection_parent
end

#slice!(index, length = 1) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/polished/knockout/knockout_array.rb', line 73

def slice!(index, length=1)
  ret = super(index, length)
  
  unless ret == nil
    `self.ko_observable.splice(index, length)`
  end
  
  ret
end

#to_jsonObject



18
19
20
# File 'lib/polished/knockout/knockout_array.rb', line 18

def to_json
  `ko.toJSON(#{@ko_observable})`
end

#to_nObject

NOTE: this method has to be run right after a KnockoutArray is first initialized



10
11
12
13
14
15
16
# File 'lib/polished/knockout/knockout_array.rb', line 10

def to_n
  array_value = super
  
  @ko_observable = `ko.observableArray(array_value)`
  
  @ko_observable
end

#unshift(obj) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/polished/knockout/knockout_array.rb', line 63

def unshift(obj)
  obj = collection_check(obj)
  
  ret = super(obj)
  
  `self.ko_observable.unshift(obj.$to_n())`
  
  ret
end