joosy 1.2.0.alpha.71 → 1.2.0.alpha.73

Sign up to get free protection for your applications and to get access to all the features.
@@ -108,9 +108,9 @@ describe "Joosy.Resources.REST", ->
108
108
  expect(item.memberPath()).toEqual '/grand_parents/1/parents/2/tests/3'
109
109
 
110
110
  it 'saves member path for instances collection', ->
111
- items = Interpolated.find [1,2,'all']
111
+ items = Interpolated.all [1,2]
112
112
  checkAndRespond @server.requests[0], 'GET', /^\/grand_parents\/1\/parents\/2\/tests\?_=\d+/, '{"tests": [{"test": {"id": 3}}]}'
113
- expect(items.at(0).memberPath()).toEqual '/grand_parents/1/parents/2/tests/3'
113
+ expect(items[0].memberPath()).toEqual '/grand_parents/1/parents/2/tests/3'
114
114
 
115
115
  describe '@collectionPath', ->
116
116
  it 'builds collection path', ->
@@ -159,33 +159,33 @@ describe "Joosy.Resources.REST", ->
159
159
  expect(resource).toBe cbResource
160
160
  checkAndRespond @server.requests[0], 'GET', /^\/fluffies\/1\?_=\d+/, rawData
161
161
 
162
- describe "@find('all')", ->
162
+ describe '@all', ->
163
163
  rawData = '{"page": 42, "fluffies": [{"id": 1, "name": "test1"}, {"id": 2, "name": "test2"}]}'
164
164
 
165
165
  beforeEach ->
166
166
  @callback = sinon.spy (error, target, data) ->
167
- expect(target instanceof Joosy.Resources.RESTCollection).toEqual true
168
- expect(target.size()).toEqual 2
169
- expect(target.at(0) instanceof Fluffy).toEqual true
167
+ expect(target instanceof Joosy.Resources.Array).toEqual true
168
+ expect(target.length).toEqual 2
169
+ expect(target[0] instanceof Fluffy).toEqual true
170
170
  expect(data).toEqual $.parseJSON(rawData)
171
171
 
172
172
  it "gets collection without params", ->
173
- resource = Fluffy.find 'all', @callback
173
+ resource = Fluffy.all @callback
174
174
  checkAndRespond @server.requests[0], 'GET', /^\/fluffies\?_=\d+/, rawData
175
175
  expect(@callback.callCount).toEqual 1
176
176
 
177
177
  it "gets collection with action", ->
178
- resource = Fluffy.find 'all', {action: 'action'}, @callback
178
+ resource = Fluffy.all {action: 'action'}, @callback
179
179
  checkAndRespond @server.requests[0], 'GET', /^\/fluffies\/action\?_=\d+/, rawData
180
180
  expect(@callback.callCount).toEqual 1
181
181
 
182
182
  it "gets collection with params", ->
183
- resource = Fluffy.find 'all', params: {foo: 'bar'}, @callback
183
+ resource = Fluffy.all {params: {foo: 'bar'}}, @callback
184
184
  checkAndRespond @server.requests[0], 'GET', /^\/fluffies\?foo=bar&_=\d+/, rawData
185
185
  expect(@callback.callCount).toEqual 1
186
186
 
187
187
  it 'gets collection with url', ->
188
- resource = Fluffy.find 'all', url: '/some/custom/url', @callback
188
+ resource = Fluffy.all url: '/some/custom/url', @callback
189
189
  checkAndRespond @server.requests[0], 'GET', /^\/some\/custom\/url\?_=\d+/, rawData
190
190
  expect(@callback.callCount).toEqual 1
191
191
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: joosy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0.alpha.71
4
+ version: 1.2.0.alpha.73
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boris Staal
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-09-03 00:00:00.000000000 Z
13
+ date: 2013-09-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sprockets
@@ -64,10 +64,8 @@ files:
64
64
  - source/joosy/extensions/resources-form/helpers/form.coffee
65
65
  - source/joosy/extensions/resources-form/index.coffee
66
66
  - source/joosy/extensions/resources/base.coffee
67
- - source/joosy/extensions/resources/collection.coffee
68
67
  - source/joosy/extensions/resources/index.coffee
69
68
  - source/joosy/extensions/resources/rest.coffee
70
- - source/joosy/extensions/resources/rest_collection.coffee
71
69
  - source/joosy/helpers/routes.coffee
72
70
  - source/joosy/helpers/view.coffee
73
71
  - source/joosy/helpers/widgets.coffee
@@ -82,11 +80,12 @@ files:
82
80
  - source/joosy/modules/page/scrolling.coffee
83
81
  - source/joosy/modules/page/title.coffee
84
82
  - source/joosy/modules/renderer.coffee
83
+ - source/joosy/modules/resources.coffee
84
+ - source/joosy/modules/resources/cacher.coffee
85
85
  - source/joosy/modules/time_manager.coffee
86
86
  - source/joosy/modules/widgets_manager.coffee
87
87
  - source/joosy/page.coffee
88
88
  - source/joosy/resources/array.coffee
89
- - source/joosy/resources/cacher.coffee
90
89
  - source/joosy/resources/hash.coffee
91
90
  - source/joosy/resources/scalar.coffee
92
91
  - source/joosy/router.coffee
@@ -107,8 +106,12 @@ files:
107
106
  - spec/joosy/core/modules/log_spec.coffee
108
107
  - spec/joosy/core/modules/module_spec.coffee
109
108
  - spec/joosy/core/modules/renderer_spec.coffee
109
+ - spec/joosy/core/modules/resources/cacher_spec.coffee
110
110
  - spec/joosy/core/modules/time_manager_spec.coffee
111
111
  - spec/joosy/core/page_spec.coffee
112
+ - spec/joosy/core/resources/array_spec.coffee
113
+ - spec/joosy/core/resources/hash_spec.coffee
114
+ - spec/joosy/core/resources/scalar_spec.coffee
112
115
  - spec/joosy/core/router_spec.coffee
113
116
  - spec/joosy/core/templaters/jst_spec.coffee
114
117
  - spec/joosy/core/widget_spec.coffee
@@ -117,8 +120,6 @@ files:
117
120
  - spec/joosy/extensions/form/form_spec.coffee
118
121
  - spec/joosy/extensions/form/helpers/forms_spec.coffee
119
122
  - spec/joosy/extensions/resources/base_spec.coffee
120
- - spec/joosy/extensions/resources/collection_spec.coffee
121
- - spec/joosy/extensions/resources/rest_collection_spec.coffee
122
123
  - spec/joosy/extensions/resources/rest_spec.coffee
123
124
  - spec/support/test.js
124
125
  - tasks/joosy.coffee
@@ -1,178 +0,0 @@
1
- #
2
- # Basic collection of Resources.
3
- # Turns JSON array into array of Resources and manages them.
4
- #
5
- # @note You should not use Collection directly. It will be
6
- # automatically created by things like {Joosy.Resources.Base.map}
7
- # or {Joosy.Resources.REST.find}.
8
- #
9
- # @example Basic sample
10
- # class R extends Joosy.Resources.Base
11
- # @entity 'r'
12
- #
13
- # collection = new Joosy.Resources.Collection(R)
14
- #
15
- # collection.load [{foo: 'bar'}, {foo: 'baz'}]
16
- # collection.each (resource) ->
17
- # resource('foo')
18
- #
19
- # @include Joosy.Modules.Events
20
- #
21
- class Joosy.Resources.Collection extends Joosy.Module
22
- @include Joosy.Modules.Events
23
-
24
- #
25
- # Allows to modify data before it gets stored
26
- #
27
- # @note Supposed to be used in descendants
28
- #
29
- # @param [Function] action `(Object) -> Object` to call
30
- #
31
- @beforeLoad: (action) -> @::__beforeLoad = action
32
-
33
- #
34
- # Sets the default model for collection
35
- #
36
- # @note Supposed to be used in descendants
37
- #
38
- # @param [Class] model Model class
39
- #
40
- @model: (model) -> @::model = model
41
-
42
- #
43
- # If model param was empty it will fallback to `@model`
44
- # If both param and `@model` were empty it will throw an exception.
45
- #
46
- # @param [Class] model Resource class which this collection will handle
47
- #
48
- constructor: (model=false, @findOptions) ->
49
- @model = model if model
50
-
51
- #
52
- # Modelized data storage
53
- #
54
- @data = []
55
-
56
- if !@model
57
- throw new Error "#{Joosy.Module.__className @}> model can't be empty"
58
-
59
- #
60
- # Clears the storage and attempts to import given array
61
- #
62
- # @param [Array, Hash] entities Array of entities to import.
63
- # If hash was given will seek for moodel name camelized and pluralized.
64
- # @param [Boolean] notify Indicates whether to trigger 'changed' event
65
- #
66
- # @return [Joosy.Resources.Collection] Returns self.
67
- #
68
- load: (entities, notify=true) ->
69
- if @__beforeLoad?
70
- entities = @__beforeLoad entities
71
-
72
- @data = @modelize entities
73
-
74
- @trigger 'changed' if notify
75
- this
76
-
77
- #
78
- # Turns Objects array into array of Resources
79
- #
80
- # @param [Array] collection Array of Objects
81
- #
82
- modelize: (collection) ->
83
- root = @model::__entityName.pluralize()
84
-
85
- if collection not instanceof Array
86
- collection = collection?[root.camelize(false)]
87
-
88
- if collection not instanceof Array
89
- throw new Error "Can not read incoming JSON"
90
-
91
- collection.map (x) =>
92
- @model.build x
93
-
94
- #
95
- # Calls callback for each Resource inside Collection
96
- #
97
- # @param [Function] callback `(mixed) -> mixed` to call for each Resource in collection
98
- #
99
- each: (callback) ->
100
- @data.each callback
101
-
102
- #
103
- # Returns number of Resources inside Collection
104
- #
105
- size: ->
106
- @data.length
107
-
108
- #
109
- # Gets first resource matching description (see Sugar.js Array#find)
110
- #
111
- # @param [Function] description Callback matcher
112
- #
113
- # @return [Joosy.Resources.Base]
114
- #
115
- find: (description) ->
116
- @data.find description
117
-
118
- sortBy: (params...) ->
119
- @data.sortBy params...
120
-
121
- #
122
- # Gets resource by id
123
- #
124
- # @param [Integer] id Id to find
125
- #
126
- # @return [Joosy.Resources.Base]
127
- #
128
- findById: (id) ->
129
- @data.find (x) -> x.id().toString() == id.toString()
130
-
131
- #
132
- # Gets resource by its index inside collection
133
- #
134
- # @param [Integer] i Index
135
- #
136
- # @return [Joosy.Resources.Base]
137
- #
138
- at: (i) ->
139
- @data[i]
140
-
141
- #
142
- # Removes resource from collection by its index or by === comparison
143
- #
144
- # @param [Integer] target Index
145
- # @param [Resource] target Resource by itself
146
- # @param [Boolean] notify Indicates whether to trigger 'changed' event
147
- #
148
- # @return [Joosy.Resources.Base] Removed element
149
- #
150
- remove: (target, notify=true) ->
151
- if Object.isNumber target
152
- index = target
153
- else
154
- index = @data.indexOf target
155
- if index >= 0
156
- result = @data.splice(index, 1)[0]
157
- if notify
158
- @trigger 'changed'
159
- result
160
-
161
- #
162
- # Adds resource to collection to given index or to the end
163
- #
164
- # @param [Resource] element Resource to add
165
- # @param [Integer] index Index to add to. If omited will be pushed to the end
166
- # @param [Boolean] notify Indicates whether to trigger 'changed' event
167
- #
168
- # @return [Joosy.Resources.Base] Added element
169
- #
170
- add: (element, index=false, notify=true) ->
171
- if typeof index is 'number'
172
- @data.splice index, 0, element
173
- else
174
- @data.push element
175
-
176
- if notify
177
- @trigger 'changed'
178
- element
@@ -1,33 +0,0 @@
1
- #= require ./collection
2
-
3
- #
4
- # Collection of REST Resources
5
- #
6
- # @include Joosy.Modules.Log
7
- # @include Joosy.Modules.Events
8
- #
9
- class Joosy.Resources.RESTCollection extends Joosy.Resources.Collection
10
- @include Joosy.Modules.Log
11
- @include Joosy.Modules.Events
12
-
13
- #
14
- # Refetches the data from backend and triggers `changed`
15
- #
16
- # @param [Hash] options See {Joosy.Resources.REST.find} for possible options
17
- # @param [Function] callback Resulting callback
18
- # @param [Object] callback `(error, instance, data) -> ...`
19
- #
20
- reload: (options={}, callback=false) ->
21
- if Object.isFunction(options)
22
- callback = options
23
- options = {}
24
-
25
- @model.__query @model.collectionPath(options, @__source), 'GET', options.params, (error, data, xhr) =>
26
- @load data if data?
27
- callback?(error, @, data, xhr)
28
-
29
- load: (args...) ->
30
- res = super(args...)
31
- @data.each (x) =>
32
- x.__source = @__source
33
- res
@@ -1,41 +0,0 @@
1
- class Joosy.Resources.Cacher extends Joosy.Module
2
-
3
- @include Joosy.Modules.Events
4
- @include Joosy.Modules.Filters
5
-
6
- @registerPlainFilters 'beforeLoad'
7
-
8
- @cache: (cacheKey) -> @::__cacheKey = cacheKey
9
- @fetcher: (fetcher) -> @::__fetcher = fetcher
10
-
11
- constructor: (callback, cacheKey=false, fetcher=false) ->
12
- if typeof(cacheKey) == 'function'
13
- fetcher = cacheKey
14
- cacheKey = undefined
15
-
16
- @__fetcher = fetcher if fetcher
17
- @__cacheKey = cacheKey if cacheKey
18
-
19
- if @__cacheKey && localStorage && localStorage[@__cacheKey]
20
- @data = @__applyBeforeLoads(JSON.parse localStorage[@__cacheKey])
21
- callback? @
22
- @refresh()
23
- else
24
- @refresh callback
25
-
26
- clone: (callback) ->
27
- copy = new @constructor(callback, @__cacheKey, @__fetcher)
28
- copy.data = Object.clone(@data, true)
29
- copy.trigger 'changed'
30
- copy
31
-
32
- refresh: (callback) ->
33
- @__fetcher (result) =>
34
- localStorage[@__cacheKey] = JSON.stringify(result) if @__cacheKey && localStorage
35
- @data = @__applyBeforeLoads result
36
- callback? @
37
- @trigger 'changed'
38
-
39
- # AMD wrapper
40
- if define?.amd?
41
- define 'joosy/resources/cacher', -> Joosy.Resources.Cacher
@@ -1,92 +0,0 @@
1
- describe "Joosy.Resources.Collection", ->
2
-
3
- beforeEach ->
4
- Joosy.Resources.Base?.resetIdentity()
5
-
6
- class Test extends Joosy.Resources.Base
7
- @entity 'test'
8
-
9
- data = '[{"id": 1, "name": "test1"}, {"id": 2, "name": "test2"}]'
10
-
11
- checkData = (collection) ->
12
- expect(collection.data.length).toEqual 2
13
- expect(collection.data[0].constructor == Test).toBeTruthy()
14
- expect(collection.data[0].data.name).toEqual 'test1'
15
-
16
- beforeEach ->
17
- @collection = new Joosy.Resources.Collection(Test)
18
-
19
- it "should initialize", ->
20
- expect(@collection.model).toEqual Test
21
- expect(@collection.data).toEqual []
22
-
23
- it "should modelize", ->
24
- result = @collection.modelize $.parseJSON(data)
25
- expect(result[0].constructor == Test).toBeTruthy()
26
- expect(result[0].data.name).toEqual 'test1'
27
-
28
- it "should load", ->
29
- @collection.load $.parseJSON(data)
30
- checkData @collection
31
-
32
- it "should trigger changes", ->
33
- @collection.bind 'changed', callback = sinon.spy()
34
- @collection.load $.parseJSON(data)
35
- expect(callback.callCount).toEqual 1
36
-
37
- it "should not trigger changes", ->
38
- @collection.bind 'changed', callback = sinon.spy()
39
- @collection.load $.parseJSON(data), false
40
- expect(callback.callCount).toEqual 0
41
-
42
- it "should properly handle the before filter", ->
43
- class RC extends Joosy.Resources.Collection
44
- @beforeLoad (data) ->
45
- data.each (entry, i) ->
46
- data[i].tested = true
47
- data
48
-
49
- collection = new RC(Test)
50
- collection.load $.parseJSON(data)
51
-
52
- expect(collection.at(0)('tested')).toBeTruthy()
53
-
54
- it "should remove item from collection", ->
55
- @collection.load $.parseJSON(data)
56
- @collection.bind 'changed', callback = sinon.spy()
57
- @collection.remove @collection.data[1]
58
- expect(@collection.data.length).toEqual 1
59
- @collection.remove 0
60
- expect(@collection.data.length).toEqual 0
61
- expect(callback.callCount).toEqual 2
62
-
63
- it "should silently remove item from collection", ->
64
- @collection.load $.parseJSON(data)
65
- @collection.bind 'changed', callback = sinon.spy()
66
- @collection.remove @collection.data[1], false
67
- expect(@collection.data.length).toEqual 1
68
- @collection.remove 0, false
69
- expect(@collection.data.length).toEqual 0
70
- expect(callback.callCount).toEqual 0
71
-
72
- it "should add item to collection", ->
73
- @collection.load $.parseJSON(data)
74
- @collection.bind 'changed', callback = sinon.spy()
75
- @collection.add new Test {'rocking': 'mocking'}
76
- expect(@collection.data.length).toEqual 3
77
- expect(@collection.at(2).data).toEqual {'rocking': 'mocking'}
78
- @collection.add new Test({'happy': 'stubbing'}), 0
79
- expect(@collection.data.length).toEqual 4
80
- expect(@collection.at(0).data).toEqual {'happy': 'stubbing'}
81
- expect(@collection.at(3).data).toEqual {'rocking': 'mocking'}
82
- @collection.add new Test({'nerdy': 'coding'}), 1
83
- expect(@collection.data.length).toEqual 5
84
- expect(@collection.at(0).data).toEqual {'happy': 'stubbing'}
85
- expect(@collection.at(1).data).toEqual {'nerdy': 'coding'}
86
- expect(@collection.at(4).data).toEqual {'rocking': 'mocking'}
87
-
88
- it "should find items by id", ->
89
- @collection.load $.parseJSON(data)
90
-
91
- expect(@collection.findById 1).toEqual @collection.data[0]
92
- expect(@collection.findById 2).toEqual @collection.data[1]
@@ -1,41 +0,0 @@
1
- describe "Joosy.Resources.RESTCollection", ->
2
-
3
- beforeEach ->
4
- Joosy.Resources.Base?.resetIdentity()
5
-
6
- class Test extends Joosy.Resources.REST
7
- @entity 'test'
8
-
9
- beforeEach ->
10
- @server = sinon.fakeServer.create()
11
- @collection = new Joosy.Resources.RESTCollection(Test)
12
-
13
- afterEach ->
14
- @server.restore()
15
-
16
- it "loads", ->
17
- @collection.load [{"id": 1, "name": "test1"}, {"id": 2, "name": "test2"}]
18
- expect(@collection.data.length).toEqual 2
19
- expect(@collection.data[0] instanceof Test).toBeTruthy()
20
- expect(@collection.data[0].data.name).toEqual 'test1'
21
-
22
- it "reloads", ->
23
- @collection.load [{"id": 1, "name": "test1"}, {"id": 2, "name": "test2"}]
24
- @collection.reload action: 'test'
25
-
26
- target = @server.requests.last()
27
- expect(target.method).toEqual 'GET'
28
- expect(target.url).toMatch /^\/tests\/test\?_=\d+/
29
- target.respond 200, 'Content-Type': 'application/json', '[{"id": 3, "name": "test3"}, {"id": 4, "name": "test4"}, {"id": 5, "name": "test5"}]'
30
-
31
- expect(@collection.data.length).toEqual 3
32
- expect(@collection.data[0].constructor == Test).toBeTruthy()
33
- expect(@collection.data[0].data.name).toEqual 'test3'
34
-
35
- it "should use own storage", ->
36
- class TestsCollection extends Joosy.Resources.RESTCollection
37
- @model Test
38
- collection = new TestsCollection()
39
- collection.add 'test'
40
- expect(collection.data).toEqual ['test']
41
- expect(collection.hasOwnProperty 'data').toBeTruthy()