modularity-rails 0.23.0 → 0.24.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,6 +36,17 @@ class modularity.PersistenceManager
36
36
  @server_data.add_all data
37
37
 
38
38
 
39
+ # Returns an array containing only the changed objects.
40
+ # Each object contains only the changed columns + key column.
41
+ changed_objects_columns: (objects) ->
42
+ modularity.tap [], (result) =>
43
+ for object in objects
44
+ diff = modularity.object_diff @server_data.get(object[@key]), object
45
+ continue if modularity.object_length(diff) == 0
46
+ diff[@key] = object[@key]
47
+ result.push(diff)
48
+
49
+
39
50
  # Returns the URL to access the collection of objects.
40
51
  collection_url: ->
41
52
  "#{@base_url}.json"
@@ -84,6 +95,11 @@ class modularity.PersistenceManager
84
95
  "#{@base_url}/#{entry[@key]}.json"
85
96
 
86
97
 
98
+ # Returns the url to access the collection of entries.
99
+ entries_url: ->
100
+ "#{@base_url}.json"
101
+
102
+
87
103
  # Returns the cached data object, or undefined.
88
104
  get_cached: (key) ->
89
105
 
@@ -160,13 +176,13 @@ class modularity.PersistenceManager
160
176
  # and have a proper value in the key attribute.
161
177
  update: (obj, callback) ->
162
178
 
179
+ # Handle updating several objects.
180
+ return @update_many(obj, callback) if $.type(obj) == 'array'
181
+
163
182
  # Create a new hash, containing only the changed attributes between obj and it's replica in @server_data.
164
183
  diff_obj = modularity.object_diff @server_data.get(obj[@key]), obj
165
184
  return if modularity.object_length(diff_obj) == 0
166
185
 
167
- # Update server_data version.
168
- @server_data.add obj
169
-
170
186
  # Send to server
171
187
  data = {}
172
188
  data[@model_name] = diff_obj
@@ -181,3 +197,27 @@ class modularity.PersistenceManager
181
197
  @client_data.add client_obj
182
198
  callback client_obj if callback
183
199
 
200
+
201
+ # Bulk-updates the given objects.
202
+ # All the existing objects must exist on the server already,
203
+ # and have a value in the key attribute.
204
+ update_many: (objects, callback) ->
205
+
206
+ # Find all objects that have been changed on the client.
207
+ diff_objects = @changed_objects_columns objects
208
+
209
+ # Send to server
210
+ data = {}
211
+ data["#{@model_name}s"] = diff_objects
212
+ jQuery.ajax
213
+ url: @entries_url()
214
+ type: 'PUT'
215
+ data: data
216
+ success: (server_response) =>
217
+ server_objects = server_response["#{@model_name}s"]
218
+ client_objects = (for server_object in server_objects
219
+ @server_data.add server_object
220
+ client_object = modularity.clone_hash server_object
221
+ @client_data.add client_object
222
+ client_object)
223
+ callback(client_objects) if callback
@@ -23,3 +23,9 @@ modularity.object_diff = (obj_1, obj_2) ->
23
23
  modularity.object_length = (obj) ->
24
24
  Object.keys(obj).length
25
25
 
26
+
27
+ # Allows to populate the given object through a callback.
28
+ modularity.tap = (obj, callback) ->
29
+ callback obj
30
+ obj
31
+
@@ -1,3 +1,3 @@
1
1
  module ModularityRails
2
- VERSION = '0.23.0'
2
+ VERSION = '0.24.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modularity-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.23.0
4
+ version: 0.24.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-02 00:00:00.000000000 Z
12
+ date: 2013-04-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails