polished-knockout 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/polished/knockout.rb +1 -0
- data/lib/polished/knockout/knockout_array.rb +112 -0
- data/lib/polished/knockout/version.rb +1 -1
- data/lib/polished/knockout/view_model.rb +0 -91
- data/spec/view_model_spec.rb +10 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22c82fbabf55ee14406eec30e735856b6296ba64
|
4
|
+
data.tar.gz: 394a99a9bd4e49cdcadddede728915f30ff725b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83733a8e81dcb4057b70c08fe7b36e0e7fb231af826d449c69b7d0d9261aac72e647edb74900920e9ec8455308eb9c263c2fc0bbb7576173f21dd4a0cfaf6cc2
|
7
|
+
data.tar.gz: a9110c214a3ccf823361fa5a2bb467e4287a522575d869aa6cdaf85044de838a88449e3b10ebe68898928e3f7e86bfa3c3c4f86d7e3d5c40ca4baa3deee7c437
|
data/lib/polished/knockout.rb
CHANGED
@@ -0,0 +1,112 @@
|
|
1
|
+
# This class is used internally and you shouldn't need to initialize it
|
2
|
+
# yourself or worry too much whether an array is a standard array or a
|
3
|
+
# KnockoutArray
|
4
|
+
#
|
5
|
+
# It's essentially a wrapper around KO's observableArray
|
6
|
+
# http://knockoutjs.com/documentation/observableArrays.html
|
7
|
+
class KnockoutArray < Array
|
8
|
+
# NOTE: this method has to be run right after a KnockoutArray is first
|
9
|
+
# initialized
|
10
|
+
def to_n
|
11
|
+
array_value = super
|
12
|
+
|
13
|
+
@ko_observable = `ko.observableArray(array_value)`
|
14
|
+
|
15
|
+
@ko_observable
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_json
|
19
|
+
`ko.toJSON(#{@ko_observable})`
|
20
|
+
end
|
21
|
+
|
22
|
+
def serialize_js_data
|
23
|
+
`ko.toJS(#{@ko_observable})`
|
24
|
+
end
|
25
|
+
|
26
|
+
def set_collection_class(class_name, collection_parent)
|
27
|
+
@collection_class_name = class_name if class_name
|
28
|
+
@collection_parent = collection_parent
|
29
|
+
end
|
30
|
+
|
31
|
+
def collection_check(obj)
|
32
|
+
if @collection_class_name and obj.is_a?(Hash)
|
33
|
+
Kernel.const_get(@collection_class_name).new_via_collection(obj, @collection_parent)
|
34
|
+
else
|
35
|
+
obj
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def concat(other)
|
40
|
+
if Array === other
|
41
|
+
other = other.to_a
|
42
|
+
else
|
43
|
+
other = Opal.coerce_to(other, Array, :to_ary).to_a
|
44
|
+
end
|
45
|
+
|
46
|
+
other.each do |item|
|
47
|
+
self.send('<<', item)
|
48
|
+
end
|
49
|
+
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def <<(obj)
|
54
|
+
obj = collection_check(obj)
|
55
|
+
|
56
|
+
ret = super(obj)
|
57
|
+
|
58
|
+
`self.ko_observable.push(obj.$to_n())`
|
59
|
+
|
60
|
+
ret
|
61
|
+
end
|
62
|
+
|
63
|
+
def unshift(obj)
|
64
|
+
obj = collection_check(obj)
|
65
|
+
|
66
|
+
ret = super(obj)
|
67
|
+
|
68
|
+
`self.ko_observable.unshift(obj.$to_n())`
|
69
|
+
|
70
|
+
ret
|
71
|
+
end
|
72
|
+
|
73
|
+
def slice!(index, length=1)
|
74
|
+
ret = super(index, length)
|
75
|
+
|
76
|
+
unless ret == nil
|
77
|
+
`self.ko_observable.splice(index, length)`
|
78
|
+
end
|
79
|
+
|
80
|
+
ret
|
81
|
+
end
|
82
|
+
|
83
|
+
def delete_at(index)
|
84
|
+
ret = super(index)
|
85
|
+
|
86
|
+
unless ret == nil
|
87
|
+
`self.ko_observable.splice(index, 1)`
|
88
|
+
end
|
89
|
+
|
90
|
+
ret
|
91
|
+
end
|
92
|
+
|
93
|
+
def delete(obj)
|
94
|
+
obj_index = index(obj)
|
95
|
+
ret = nil
|
96
|
+
|
97
|
+
unless obj_index == nil
|
98
|
+
ret = super(obj)
|
99
|
+
`self.ko_observable.splice(obj_index, 1)`
|
100
|
+
end
|
101
|
+
|
102
|
+
ret
|
103
|
+
end
|
104
|
+
|
105
|
+
def clear()
|
106
|
+
ret = super
|
107
|
+
|
108
|
+
`self.ko_observable.removeAll()`
|
109
|
+
|
110
|
+
ret
|
111
|
+
end
|
112
|
+
end
|
@@ -252,95 +252,4 @@ module Knockout
|
|
252
252
|
end
|
253
253
|
end
|
254
254
|
end
|
255
|
-
end
|
256
|
-
|
257
|
-
# This class is used internally and you shouldn't need to initialize it
|
258
|
-
# yourself or worry too much whether an array is a standard array or a
|
259
|
-
# KnockoutArray
|
260
|
-
#
|
261
|
-
class KnockoutArray < Array
|
262
|
-
# NOTE: this method has to be run right after a KnockoutArray is first
|
263
|
-
# initialized
|
264
|
-
def to_n
|
265
|
-
array_value = super
|
266
|
-
|
267
|
-
@ko_observable = `ko.observableArray(array_value)`
|
268
|
-
|
269
|
-
@ko_observable
|
270
|
-
end
|
271
|
-
|
272
|
-
def to_json
|
273
|
-
`ko.toJSON(#{@ko_observable})`
|
274
|
-
end
|
275
|
-
|
276
|
-
def serialize_js_data
|
277
|
-
`ko.toJS(#{@ko_observable})`
|
278
|
-
end
|
279
|
-
|
280
|
-
def set_collection_class(class_name, collection_parent)
|
281
|
-
@collection_class_name = class_name if class_name
|
282
|
-
@collection_parent = collection_parent
|
283
|
-
end
|
284
|
-
|
285
|
-
def collection_check(obj)
|
286
|
-
if @collection_class_name and obj.is_a?(Hash)
|
287
|
-
Kernel.const_get(@collection_class_name).new_via_collection(obj, @collection_parent)
|
288
|
-
else
|
289
|
-
obj
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
def concat(other)
|
294
|
-
if Array === other
|
295
|
-
other = other.to_a
|
296
|
-
else
|
297
|
-
other = Opal.coerce_to(other, Array, :to_ary).to_a
|
298
|
-
end
|
299
|
-
|
300
|
-
other.each do |item|
|
301
|
-
self.send('<<', item)
|
302
|
-
end
|
303
|
-
|
304
|
-
self
|
305
|
-
end
|
306
|
-
|
307
|
-
def <<(obj)
|
308
|
-
obj = collection_check(obj)
|
309
|
-
|
310
|
-
ret = super(obj)
|
311
|
-
|
312
|
-
`self.ko_observable.push(obj.$to_n())`
|
313
|
-
|
314
|
-
ret
|
315
|
-
end
|
316
|
-
|
317
|
-
def delete_at(index)
|
318
|
-
ret = super(index)
|
319
|
-
|
320
|
-
unless ret == nil
|
321
|
-
`self.ko_observable.splice(index, 1)`
|
322
|
-
end
|
323
|
-
|
324
|
-
ret
|
325
|
-
end
|
326
|
-
|
327
|
-
def delete(obj)
|
328
|
-
obj_index = index(obj)
|
329
|
-
ret = nil
|
330
|
-
|
331
|
-
unless obj_index == nil
|
332
|
-
ret = super(obj)
|
333
|
-
`self.ko_observable.splice(obj_index, 1)`
|
334
|
-
end
|
335
|
-
|
336
|
-
ret
|
337
|
-
end
|
338
|
-
|
339
|
-
def clear()
|
340
|
-
ret = super
|
341
|
-
|
342
|
-
`self.ko_observable.removeAll()`
|
343
|
-
|
344
|
-
ret
|
345
|
-
end
|
346
255
|
end
|
data/spec/view_model_spec.rb
CHANGED
@@ -114,6 +114,15 @@ describe 'bindings with view collections' do
|
|
114
114
|
|
115
115
|
view_model.items << TestCollectionItem.new.tap{|item| item.parent_view = view_model; item.title = "Name 4" }
|
116
116
|
|
117
|
-
expect(Element.find('#bindcoltest > ul > li:eq(3) > span').text).to eq('Name 4')
|
117
|
+
expect(Element.find('#bindcoltest > ul > li:eq(3) > span').text).to eq('Name 4')
|
118
|
+
|
119
|
+
view_model.items.unshift TestCollectionItem.new.tap{|item| item.parent_view = view_model; item.title = "Name 0" }
|
120
|
+
|
121
|
+
expect(Element.find('#bindcoltest > ul > li:eq(0) > span').text).to eq('Name 0')
|
122
|
+
|
123
|
+
sliced_items = view_model.items.slice!(1, 2)
|
124
|
+
|
125
|
+
expect(sliced_items[1].title).to eq('Name 2')
|
126
|
+
expect(Element.find('#bindcoltest > ul > li:eq(1) > span').text).to eq('Name 3')
|
118
127
|
end
|
119
128
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polished-knockout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jared White
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: opal
|
@@ -98,6 +98,7 @@ files:
|
|
98
98
|
- lib/polished-knockout.rb
|
99
99
|
- lib/polished/knockout.rb
|
100
100
|
- lib/polished/knockout/helpers/knockout_helpers.rb
|
101
|
+
- lib/polished/knockout/knockout_array.rb
|
101
102
|
- lib/polished/knockout/railtie.rb
|
102
103
|
- lib/polished/knockout/version.rb
|
103
104
|
- lib/polished/knockout/view_model.rb
|