joosy 0.1.0.RC3 → 0.1.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. data/.gitignore +0 -2
  2. data/Gemfile +2 -13
  3. data/Gemfile.lock +62 -80
  4. data/Guardfile +15 -21
  5. data/MIT-LICENSE +2 -2
  6. data/README.rdoc +3 -0
  7. data/app/helpers/joosy/sprockets_helper.rb +11 -23
  8. data/joosy.gemspec +3 -3
  9. data/lib/joosy/forms.rb +12 -2
  10. data/lib/joosy/rails/engine.rb +1 -9
  11. data/lib/joosy/rails/version.rb +2 -2
  12. data/lib/joosy-rails.rb +5 -0
  13. data/lib/rails/generators/joosy/application_generator.rb +3 -13
  14. data/lib/rails/generators/joosy/joosy_base.rb +2 -2
  15. data/lib/rails/generators/joosy/layout_generator.rb +1 -8
  16. data/lib/rails/generators/joosy/page_generator.rb +6 -21
  17. data/lib/rails/generators/joosy/preloader_generator.rb +7 -14
  18. data/lib/rails/generators/joosy/templates/app/layouts/application.js.coffee +0 -1
  19. data/lib/rails/generators/joosy/templates/app/layouts/template.js.coffee +1 -1
  20. data/lib/rails/generators/joosy/templates/app/pages/application.js.coffee +1 -1
  21. data/lib/rails/generators/joosy/templates/app/pages/template.js.coffee +3 -3
  22. data/lib/rails/generators/joosy/templates/app/routes.js.coffee +1 -7
  23. data/lib/rails/generators/joosy/templates/app/widgets/template.js.coffee +2 -2
  24. data/lib/rails/generators/joosy/templates/app.js.coffee +0 -3
  25. data/lib/rails/generators/joosy/templates/app_preloader.js.coffee.erb +12 -9
  26. data/lib/rails/generators/joosy/templates/preload.html.erb +6 -5
  27. data/lib/rails/generators/joosy/templates/preload.html.haml +5 -3
  28. data/lib/rails/generators/joosy/templates/preload.html.slim +21 -0
  29. data/lib/rails/generators/joosy/widget_generator.rb +1 -8
  30. data/spec/javascripts/helpers/spec_helper.js.coffee +0 -20
  31. data/spec/javascripts/joosy/core/modules/container_spec.js.coffee +27 -24
  32. data/spec/javascripts/joosy/core/modules/events_spec.js.coffee +18 -42
  33. data/spec/javascripts/joosy/core/modules/filters_spec.js.coffee +27 -27
  34. data/spec/javascripts/joosy/core/modules/log_spec.js.coffee +3 -3
  35. data/spec/javascripts/joosy/core/modules/module_spec.js.coffee +15 -6
  36. data/spec/javascripts/joosy/core/modules/time_manager_spec.js.coffee +7 -12
  37. data/spec/javascripts/joosy/core/modules/widget_manager_spec.js.coffee +17 -31
  38. data/tmp/javascripts/.gitignore +1 -0
  39. data/tmp/spec/javascripts/helpers/.gitignore +1 -0
  40. data/vendor/assets/javascripts/base64.js +135 -0
  41. data/vendor/assets/javascripts/joosy/core/application.js.coffee +26 -0
  42. data/vendor/assets/javascripts/joosy/core/form.js.coffee +87 -0
  43. data/vendor/assets/javascripts/joosy/core/joosy.js.coffee +62 -0
  44. data/vendor/assets/javascripts/joosy/core/layout.js.coffee +38 -0
  45. data/vendor/assets/javascripts/joosy/core/modules/container.js.coffee +51 -0
  46. data/vendor/assets/javascripts/joosy/core/modules/events.js.coffee +17 -0
  47. data/vendor/assets/javascripts/joosy/core/modules/filters.js.coffee +39 -0
  48. data/vendor/assets/javascripts/joosy/core/modules/log.js.coffee +12 -0
  49. data/vendor/assets/javascripts/joosy/core/modules/module.js.coffee +28 -0
  50. data/vendor/assets/javascripts/joosy/core/modules/time_manager.js.coffee +23 -0
  51. data/vendor/assets/javascripts/joosy/core/modules/widgets_manager.js.coffee +45 -0
  52. data/vendor/assets/javascripts/joosy/core/page.js.coffee +136 -0
  53. data/vendor/assets/javascripts/joosy/core/resource/rest.js.coffee +69 -0
  54. data/vendor/assets/javascripts/joosy/core/resource/rest_collection.js.coffee +42 -0
  55. data/vendor/assets/javascripts/joosy/core/router.js.coffee +75 -0
  56. data/vendor/assets/javascripts/joosy/core/widget.js.coffee +35 -0
  57. data/vendor/assets/javascripts/joosy/preloader/development.js.coffee +27 -0
  58. data/vendor/assets/javascripts/joosy/preloader/production.js.coffee +84 -0
  59. data/{app → vendor}/assets/javascripts/joosy.js.coffee +0 -0
  60. data/vendor/assets/javascripts/jquery.form.js +963 -978
  61. data/vendor/assets/javascripts/sugar.js +1 -1
  62. metadata +48 -77
  63. data/.codoopts +0 -5
  64. data/README.md +0 -95
  65. data/app/assets/javascripts/joosy/core/application.js.coffee +0 -50
  66. data/app/assets/javascripts/joosy/core/form.js.coffee +0 -304
  67. data/app/assets/javascripts/joosy/core/helpers/view.js.coffee +0 -30
  68. data/app/assets/javascripts/joosy/core/joosy.js.coffee +0 -129
  69. data/app/assets/javascripts/joosy/core/layout.js.coffee +0 -162
  70. data/app/assets/javascripts/joosy/core/modules/container.js.coffee +0 -104
  71. data/app/assets/javascripts/joosy/core/modules/events.js.coffee +0 -123
  72. data/app/assets/javascripts/joosy/core/modules/filters.js.coffee +0 -67
  73. data/app/assets/javascripts/joosy/core/modules/log.js.coffee +0 -36
  74. data/app/assets/javascripts/joosy/core/modules/module.js.coffee +0 -105
  75. data/app/assets/javascripts/joosy/core/modules/renderer.js.coffee +0 -195
  76. data/app/assets/javascripts/joosy/core/modules/time_manager.js.coffee +0 -46
  77. data/app/assets/javascripts/joosy/core/modules/widgets_manager.js.coffee +0 -84
  78. data/app/assets/javascripts/joosy/core/page.js.coffee +0 -375
  79. data/app/assets/javascripts/joosy/core/preloader.js.coffee +0 -13
  80. data/app/assets/javascripts/joosy/core/resource/collection.js.coffee +0 -174
  81. data/app/assets/javascripts/joosy/core/resource/generic.js.coffee +0 -254
  82. data/app/assets/javascripts/joosy/core/resource/rest.js.coffee +0 -197
  83. data/app/assets/javascripts/joosy/core/resource/rest_collection.js.coffee +0 -125
  84. data/app/assets/javascripts/joosy/core/router.js.coffee +0 -163
  85. data/app/assets/javascripts/joosy/core/templaters/rails_jst.js.coffee +0 -36
  86. data/app/assets/javascripts/joosy/core/widget.js.coffee +0 -80
  87. data/app/assets/javascripts/joosy/preloaders/caching.js.coffee +0 -169
  88. data/app/assets/javascripts/joosy/preloaders/inline.js.coffee +0 -56
  89. data/lib/joosy.rb +0 -9
  90. data/lib/rails/generators/joosy/resource_generator.rb +0 -29
  91. data/lib/rails/generators/joosy/templates/app/helpers/application.js.coffee +0 -4
  92. data/lib/rails/generators/joosy/templates/app/pages/welcome/index.js.coffee +0 -22
  93. data/lib/rails/generators/joosy/templates/app/resources/template.js.coffee +0 -2
  94. data/lib/rails/generators/joosy/templates/app/templates/layouts/application.jst.hamlc +0 -2
  95. data/lib/rails/generators/joosy/templates/app/templates/pages/welcome/index.jst.hamlc +0 -7
  96. data/spec/javascripts/joosy/core/application_spec.js.coffee +0 -40
  97. data/spec/javascripts/joosy/core/form_spec.js.coffee +0 -184
  98. data/spec/javascripts/joosy/core/joosy_spec.js.coffee +0 -72
  99. data/spec/javascripts/joosy/core/layout_spec.js.coffee +0 -50
  100. data/spec/javascripts/joosy/core/modules/renderer_spec.js.coffee +0 -185
  101. data/spec/javascripts/joosy/core/page_spec.js.coffee +0 -178
  102. data/spec/javascripts/joosy/core/resource/collection_spec.js.coffee +0 -84
  103. data/spec/javascripts/joosy/core/resource/generic_spec.js.coffee +0 -118
  104. data/spec/javascripts/joosy/core/resource/rest_collection_spec.js.coffee +0 -58
  105. data/spec/javascripts/joosy/core/resource/rest_spec.js.coffee +0 -129
  106. data/spec/javascripts/joosy/core/router_spec.js.coffee +0 -123
  107. data/spec/javascripts/joosy/core/templaters/rails_jst_spec.js.coffee +0 -25
  108. data/spec/javascripts/joosy/core/widget_spec.js.coffee +0 -40
  109. data/spec/javascripts/joosy/preloaders/caching_spec.js.coffee +0 -36
  110. data/spec/javascripts/joosy/preloaders/inline_spec.js.coffee +0 -16
  111. data/spec/javascripts/support/assets/coolface.jpg +0 -0
  112. data/spec/javascripts/support/assets/okay.jpg +0 -0
  113. data/spec/javascripts/support/assets/test.js +0 -1
  114. data/spec/javascripts/support/sinon-ie-1.3.1.js +0 -82
  115. data/vendor/assets/javascripts/metamorph.js +0 -409
@@ -1,254 +0,0 @@
1
- #
2
- # Basic data wrapper with triggering and entity name binding
3
- #
4
- # @example Basic usage
5
- # class R extends Joosy.Resource.Generic
6
- # @entity 'r'
7
- #
8
- # @beforeLoad (data) ->
9
- # data.real = true
10
- #
11
- # r = R.create {r: {foo: {bar: 'baz'}}}
12
- #
13
- # r('foo') # {baz: 'baz'}
14
- # r('real') # true
15
- # r('foo.bar') # baz
16
- # r('foo.bar', 'fluffy') # triggers 'changed'
17
- #
18
- class Joosy.Resource.Generic extends Joosy.Module
19
- @include Joosy.Modules.Log
20
- @include Joosy.Modules.Events
21
-
22
- #
23
- # Sets the data source description (which is NOT required)
24
- # @note This has no use in Generic but is required in any descendant
25
- #
26
- # @param [mixed] Source can be any type including lambda.
27
- # If lambda is given resource will not expect direct {Joosy.Resource.Generic.create} calls
28
- # You'll have to prepare descendant with {Joosy.Resource.Generic.at} first.
29
- #
30
- # @example Simple case
31
- # Class Y extends Joosy.Resource.Generic
32
- # @source 'fluffies'
33
- #
34
- # r = Y.create {}
35
- #
36
- # @example Case with lambda
37
- # class R extends Joosy.Resource.Generic
38
- # @source -> (path) "/"+path
39
- #
40
- # r = R.create {} # will raise exception
41
- # r = R.at('foo/bar').create {} # will work as expected
42
- #
43
- @source: (source) -> @__source = source
44
-
45
- #
46
- # Creates the proxy of current resource with proper {Joosy.Resource.Generic.source} value
47
- #
48
- # @note Should be used together with lambda source (see {Joosy.Resource.Generic.source} for example)
49
- #
50
- @at: ->
51
- if !Object.isFunction @__source
52
- throw new Error "#{Joosy.Module.__className @}> should be created directly (without `at')"
53
-
54
- #
55
- # Class inheritance used to create proxy
56
- #
57
- # @private
58
- #
59
- class clone extends this
60
- clone.__source = @__source arguments...
61
- clone
62
-
63
- #
64
- # Sets the entity text name:
65
- # required to do some magic like skipping the root node.
66
- #
67
- # @param [String] name Singular name of resource
68
- #
69
- @entity: (name) -> @::__entityName = name
70
-
71
- #
72
- # Sets the collection to use
73
- #
74
- # @note By default will try to seek for `EntityNamesCollection`.
75
- # Will fallback to {Joosy.Resource.Collection}
76
- #
77
- # @param [Class] klass Class to assign as collection
78
- #
79
- @collection: (klass) -> @::__collection = -> klass
80
-
81
- #
82
- # Implements {Joosy.Resource.Generic.collection} default behavior.
83
- #
84
- __collection: ->
85
- named = @__entityName.camelize().pluralize() + 'Collection'
86
- if window[named] then window[named] else Joosy.Resource.Collection
87
-
88
- #
89
- # Allows to modify data before it gets stored.
90
- # You can define several beforeLoad filters that will be chained.
91
- #
92
- # @param [Function] action `(Object) -> Object` to call
93
- #
94
- @beforeLoad: (action) ->
95
- unless @::hasOwnProperty '__beforeLoads'
96
- @::__beforeLoads = [].concat @.__super__.__beforeLoads || []
97
- @::__beforeLoads.push action
98
-
99
- #
100
- # Dynamically creates collection of inline resources.
101
- #
102
- # Inline resources share the instance with direct data and therefore can be used
103
- # to handle inline changes with triggers and all that resources stuff
104
- #
105
- # @example Basic usage
106
- # class Zombie extends Joosy.Resource.Generic
107
- # @entity 'zombie'
108
- # class Puppy extends Joosy.Resource.Generic
109
- # @entity 'puppy'
110
- # @map 'zombies'
111
- #
112
- # p = Puppy.create {zombies: [{foo: 'bar'}]}
113
- #
114
- # p('zombies') # Direct access: [{foo: 'bar'}]
115
- # p.zombies # Wrapped Collection of Zombie instances
116
- # p.zombies.at(0)('foo') # bar
117
- #
118
- # @param [String] name Pluralized name of property to define
119
- # @param [Class] klass Resource class to instantiate
120
- #
121
- @map: (name, klass=false) ->
122
- unless klass
123
- klass = window[name.singularize().camelize()]
124
-
125
- if !klass
126
- throw new Error "#{Joosy.Module.__className @}> class can not be detected for '#{name}' mapping"
127
-
128
- @beforeLoad (data) ->
129
- @[name] = new (klass::__collection()) klass
130
- if Object.isArray data[name]
131
- @[name].reset data[name]
132
- data
133
-
134
- #
135
- # Wraps instance of resource inside shim-function allowing to track
136
- # data changes. See class example
137
- #
138
- # @return [Joosy.Resource.Generic]
139
- #
140
- @create: ->
141
- shim = ->
142
- shim.__call.apply shim, arguments
143
-
144
- if shim.__proto__
145
- shim.__proto__ = @prototype
146
- else
147
- for key, value of @prototype
148
- shim[key] = value
149
-
150
- shim.constructor = @
151
-
152
- @apply shim, arguments
153
-
154
- shim
155
-
156
- #
157
- # Should NOT be called directly, use {::create} instead
158
- #
159
- # @private
160
- # @param [Object] data Data to store
161
- #
162
- constructor: (data) ->
163
- @__fillData data, false
164
-
165
- #
166
- # Set the resource data manually
167
- #
168
- # @param [Object] data Data to store
169
- #
170
- # @return [Joosy.Resource.Generic] Returns self
171
- #
172
- reset: (data) ->
173
- @__fillData data
174
- return this
175
-
176
- #
177
- # Getter for wrapped data
178
- #
179
- # @param [String] path Attribute name to get. Can contain dots to get inline Objects values
180
- # @return [mixed]
181
- #
182
- get: (path) ->
183
- target = @__callTarget path
184
- target[0][target[1]]
185
-
186
- #
187
- # Setter for wrapped data, triggers `changed` event.
188
- #
189
- # @param [String] path Attribute name to set. Can contain dots to get inline Objects values
190
- # @param [mixed] value Value to set
191
- #
192
- set: (path, value) ->
193
- target = @__callTarget path
194
- target[0][target[1]] = value
195
- @trigger 'changed'
196
- null
197
-
198
- #
199
- # Locates the actual instance of attribute path `foo.bar` from get/set
200
- #
201
- # @param [String] path Path to the attribute (`foo.bar`)
202
- # @return [Array] Instance of object containing last step of path and keyword for required field
203
- #
204
- __callTarget: (path) ->
205
- if path.has(/\./) && !@e[path]?
206
- path = path.split '.'
207
- keyword = path.pop()
208
- target = @e
209
-
210
- for part in path
211
- target[part] ||= {}
212
- target = target[part]
213
-
214
- [target, keyword]
215
- else
216
- [@e, path]
217
-
218
- #
219
- # Wrapper for {Joosy.Resource.Generic.create} magic
220
- #
221
- __call: (path, value) ->
222
- if arguments.length > 1
223
- @set path, value
224
- else
225
- @get path
226
-
227
- #
228
- # Defines how exactly prepared data should be saved
229
- #
230
- # @param [Object] data Raw data to store
231
- #
232
- __fillData: (data, notify=true) ->
233
- @e = @__prepareData data
234
-
235
- if notify
236
- @trigger 'changed'
237
-
238
- null
239
-
240
- #
241
- # Prepares raw data: cuts the root node if it exists, runs before filters
242
- #
243
- # @param [Hash] data Raw data to prepare
244
- # @return [Hash]
245
- #
246
- __prepareData: (data) ->
247
- if Object.isObject(data) && Object.keys(data).length == 1 && @__entityName
248
- name = @__entityName.camelize(false)
249
- data = data[name] if data[name]
250
-
251
- if @__beforeLoads?
252
- data = bl.call(this, data) for bl in @__beforeLoads
253
-
254
- data
@@ -1,197 +0,0 @@
1
- #= require ./rest_collection
2
-
3
- #
4
- # Resource with the HTTP REST as the backend.
5
- #
6
- # @example Basic usage
7
- # class Rocket extends Joosy.Resource.REST
8
- # @entity 'rocket'
9
- #
10
- # r = Rocket.find {speed: 'fast'} # queries /rockets/?speed=fast to get RESTCollection
11
- # r = Rocket.find 1 # queries /rockets/1 to get Rocket instance
12
- #
13
- # class Engine extends Joosy.Resource.REST
14
- # @entity 'engine'
15
- # @source ->
16
- # (rocket) "/rockets/#{rocket 'id'}/engines"
17
- #
18
- # e = Engine.at(r).find {oil: true} # queries /rockets/1/engies?oil=true
19
- #
20
- class Joosy.Resource.REST extends Joosy.Resource.Generic
21
-
22
- #
23
- # Default primary key field 'id'
24
- #
25
- __primaryKey: 'id'
26
-
27
- #
28
- # Implements `@collection` default behavior.
29
- # Changes the default fallback to Joosy.Resource.RESTCollection.
30
- #
31
- __collection: ->
32
- named = @__entityName.camelize().pluralize() + 'Collection'
33
- if window[named] then window[named] else Joosy.Resource.RESTCollection
34
-
35
- #
36
- # Sets the field containing primary key.
37
- #
38
- # @note It has no direct use inside the REST resource itself and can be omited.
39
- # But it usually should not since we have plans on adding some kind of Identity Map to Joosy.
40
- #
41
- # @param [String] primary Name of the field
42
- #
43
- @primary: (primary) -> @::__primaryKey = primary
44
-
45
- #
46
- # Should NOT be called directly, use {Joosy.Resource.Generic.create} instead
47
- #
48
- # @param [Integer, String, Object] description ID of entity or full data to store
49
- #
50
- constructor: (description={}) ->
51
- if @constructor.__isId description
52
- @id = description
53
- else
54
- super description
55
- @id = @e[@__primaryKey]
56
-
57
- #
58
- # Queries for REST data and creates resources instances.
59
- #
60
- # Returns single entity if integer or string given.
61
- # Returns collection if no value or Object (with parameters) given.
62
- #
63
- # If first parameter is a Function it's considered as a result callback,
64
- # in this case parameters will be considered equal to `{ }`
65
- #
66
- # @example Different find
67
- # class Rocket extends Joosy.Resource.REST
68
- # @entity 'rocket'
69
- #
70
- # Rocket.find 1
71
- # Rocket.find {type: 'nuclear'}, (data) -> data
72
- # Rocket.find (data) -> data
73
- # Rocket.find 1,
74
- # success: (data) -> data)
75
- # cache: true
76
- #
77
- # @param [Integer, String, Object] description ID of entity or full data to store
78
- # @param [Hash, Function] options AJAX options.
79
- # Will be considered as a success callback if function is given.
80
- #
81
- # @return [Joosy.Resource.REST, Joosy.Resource.RESTCollection]
82
- #
83
- @find: (description, options) ->
84
- if Object.isFunction options
85
- options = {success: options}
86
-
87
- if @__isId description
88
- resource = @create description
89
- resource.fetch options
90
- resource
91
- else
92
- if !options? && Object.isFunction description
93
- options = {success: description}
94
- description = undefined
95
- resources = new (@::__collection()) this, description
96
- resources.fetch options
97
- resources
98
-
99
- #
100
- # Queries the resource url and reloads the data from server
101
- #
102
- # @param [Hash, Function] options AJAX options.
103
- # Will be considered as a success callback if function is given.
104
- # @return [Joosy.Resource.REST]
105
- #
106
- fetch: (options) ->
107
- if Object.isFunction options
108
- callback = options
109
- else
110
- callback = options?.success
111
- delete options?.success
112
-
113
- @constructor.__ajax 'get', @constructor.__buildSource(extension: @id), options, (e) =>
114
- @__fillData e, false
115
- callback? this
116
- @trigger 'changed'
117
- this
118
-
119
- save: ->
120
-
121
- #
122
- # Destroys the resource by DELETE query
123
- #
124
- # @param [Hash, Function] options AJAX options.
125
- # Will be considered as a success callback if function is given.
126
- # @return [Joosy.Resource.REST]
127
- #
128
- destroy: (options) ->
129
- if Object.isFunction options
130
- callback = options
131
- else
132
- callback = options?.success
133
- delete options?.success
134
-
135
- @constructor.__ajax 'delete', @constructor.__buildSource(extension: @id), options, (e) =>
136
- callback? this
137
- this
138
-
139
- #
140
- # Requests the REST member URL with POST or any method given in options.type.
141
- #
142
- # @param [String] ending Member url (like 'foo' or 'foo/bar')
143
- # @param [Hash, Function] options AJAX options.
144
- # Will be considered as a success callback if function is given.
145
- #
146
- request: (ending, options) ->
147
- if Object.isFunction options
148
- callback = options
149
- else
150
- callback = options?.success
151
- delete options?.success
152
-
153
- if options.method || options.type
154
- type = options.method || options.type
155
- else
156
- type = 'post'
157
-
158
- @constructor.__ajax type, @constructor.__buildSource(extension: "#{@id}/#{ending}"), options, callback
159
-
160
- #
161
- # Checks if given description can be considered as ID
162
- #
163
- # @param [Integer, String, Object] something Value to test
164
- # @return [Boolean]
165
- #
166
- @__isId: (something) ->
167
- Object.isNumber(something) || Object.isString(something)
168
-
169
- #
170
- # jQuery AJAX wrapper.
171
- #
172
- # @param [String] method HTTP Method (GET/POST/PUT/DELETE)
173
- # @param [String] url URL to query
174
- # @param [Hash] options AJAX options to pass with request
175
- # @param [Function] callback XHR callback
176
- #
177
- @__ajax: (method, url, options={}, callback) ->
178
- $.ajax url, Joosy.Module.merge options,
179
- type: method
180
- success: callback
181
- cache: false
182
- dataType: 'json'
183
-
184
- #
185
- # Builds URL for current resource location.
186
- #
187
- # @param [Hash] options Handling options
188
- #
189
- # @option options [String] extension String to add to resource base url
190
- # @option options [Hash] params GET-params to add to resulting url
191
- #
192
- @__buildSource: (options={}) ->
193
- unless @hasOwnProperty '__source'
194
- @__source = "/" + @::__entityName.pluralize()
195
-
196
- source = if Object.isFunction(@__source) then @__source() else @__source
197
- source = Joosy.buildUrl "#{source}/#{options.extension || ''}", options.params
@@ -1,125 +0,0 @@
1
- #
2
- # Collection of Resources with REST-fetching capabilities.
3
- #
4
- # @note Generally you should not use RESTCollection directly. It will be
5
- # automatically created by Joosy.Resource.REST#find.
6
- #
7
- # @example Basic samples
8
- # class R extends Joosy.Resource.REST
9
- # @entity 'r'
10
- #
11
- # collection = new Joosy.Resource.RESTCollection(R, {color: 'green'})
12
- #
13
- # collection.fetch()
14
- # collection.page 2
15
- #
16
- # collection.params = {color: 'red', sort: 'date'}
17
- # collection.fetch()
18
- #
19
- class Joosy.Resource.RESTCollection extends Joosy.Resource.Collection
20
- @include Joosy.Modules.Log
21
- @include Joosy.Modules.Events
22
-
23
- #
24
- # Hash containing the cache for pages of raw data.
25
- # Keys are pages numbers, values are stored AJAX response.
26
- #
27
- pages: Object.extended()
28
-
29
- #
30
- # @param [Class] model Resource class this collection will handle
31
- # @param [Hash] params Additional parameters that will be added to all REST requests.
32
- #
33
- constructor: (model, params={}) ->
34
- super model
35
- @params = Object.extended params
36
-
37
- #
38
- # Clears the storage and attempts to import given array
39
- #
40
- # @param [Array, Hash] entities Array of entities to import.
41
- # If hash was given will seek for moodel name camelized and pluralized.
42
- # @param [Boolean] notify Indicates whether to trigger 'changed' event
43
- #
44
- # @return [Joosy.Resource.RESTCollection] Returns self.
45
- #
46
- reset: (entities, notify=true) ->
47
- super entities, false
48
- if notify
49
- @trigger 'changed'
50
- this
51
-
52
- #
53
- # Clears the storage and gets new data from server.
54
- #
55
- # @param [Hash, Function] options AJAX options.
56
- # Will be considered as a success callback if function is given.
57
- #
58
- # @return [Joosy.Resource.RESTCollection] Returns self.
59
- #
60
- fetch: (options) ->
61
- if Object.isFunction options
62
- callback = options
63
- else
64
- callback = options?.success
65
- delete options?.success
66
-
67
- @__fetch @params, options, (data) =>
68
- @reset data, false
69
- callback? this
70
- @trigger 'changed'
71
- this
72
-
73
- #
74
- # Returns the subset for requested page: requests with &page=x.
75
- #
76
- # @param [Integer] number Index of page
77
- # @param [Hash, Function] options AJAX options.
78
- # Will be considered as a success callback if function is given.
79
- #
80
- # @return [Joosy.Resource.RESTCollection] Returns self.
81
- #
82
- page: (number, options) ->
83
- if Object.isFunction options
84
- callback = options
85
- else
86
- callback = options?.success
87
- delete options?.success
88
-
89
- @__fetch Joosy.Module.merge({page: number}, @params), options, (data) =>
90
- @reset data, false
91
- callback? @data
92
- @trigger 'changed'
93
- this
94
-
95
- #
96
- # Requests the REST collection URL with POST or any method given in options.type.
97
- #
98
- # @param [String] ending Collection url (like 'foo' or 'foo/bar')
99
- # @param [Hash, Function] options AJAX options.
100
- # Will be considered as a success callback if function is given.
101
- #
102
- request: (ending, options) ->
103
- if Object.isFunction options
104
- callback = options
105
- else
106
- callback = options?.success
107
- delete options?.success
108
-
109
- if options.method || options.type
110
- type = options.method || options.type
111
- else
112
- type = 'post'
113
-
114
- @model.__ajax type, @model.__buildSource(extension: ending), options, callback
115
-
116
- #
117
- # Internal AJAX request implementation.
118
- #
119
- # @param [Hash] urlOptions GET-params for request
120
- # @param [Hash] ajaxOptions AJAX options to pass with request
121
- # @param [Function] callback
122
- #
123
- __fetch: (urlOptions, ajaxOptions, callback) ->
124
- @model.__ajax 'get', @model.__buildSource(params: @params.merge(urlOptions)), ajaxOptions, (data) ->
125
- callback(data)