modularity-rails 0.19.0 → 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,11 @@
|
|
3
3
|
#= require modularity/tools/object_tools
|
4
4
|
|
5
5
|
# Provides persistence services for data models.
|
6
|
+
#
|
7
|
+
# This class operates against a standard RESTful data API, whole path is given in params.url.
|
8
|
+
# In addition to the standard RESTful routes, the data API is expected to support these methods for performance reasons:
|
9
|
+
# * DELETE /entries: Delete a whole batch of entries at once. The ids of the entries to delete are given as a JSON hash in the body of the request.
|
10
|
+
# *
|
6
11
|
class modularity.PersistenceManager
|
7
12
|
|
8
13
|
constructor: (params) ->
|
@@ -16,8 +21,12 @@ class modularity.PersistenceManager
|
|
16
21
|
# The base url on the server. Expected to be a fully RESTful API.
|
17
22
|
@base_url = params.url
|
18
23
|
|
24
|
+
# Name of the 'id' column for models.
|
19
25
|
@key = params.key or 'id'
|
20
26
|
|
27
|
+
# Name of the model class (e.g. "user").
|
28
|
+
@model_name = params.model_name
|
29
|
+
|
21
30
|
# For handling parallel requests to the server.
|
22
31
|
@loader = new modularity.AjaxLoader { cache: no }
|
23
32
|
|
@@ -93,7 +102,7 @@ class modularity.PersistenceManager
|
|
93
102
|
load: (key, callback) ->
|
94
103
|
|
95
104
|
# Try to load from cache.
|
96
|
-
return callback(
|
105
|
+
return callback(entry) if entry = @get_cached key
|
97
106
|
|
98
107
|
# No data on client at all --> load data from server.
|
99
108
|
@loader.get "#{@base_url}/#{key}", (server_entry) =>
|
@@ -115,21 +124,21 @@ class modularity.PersistenceManager
|
|
115
124
|
callback()
|
116
125
|
|
117
126
|
|
118
|
-
# Loads all
|
127
|
+
# Loads all entries with the given ids.
|
119
128
|
load_many: (ids, callback) ->
|
120
129
|
missing_ids = []
|
121
|
-
|
130
|
+
entries = []
|
122
131
|
$.each ids, (pos, id) =>
|
123
|
-
|
124
|
-
if
|
125
|
-
|
132
|
+
entry = @get_cached id
|
133
|
+
if entry
|
134
|
+
entries.push entry
|
126
135
|
else
|
127
136
|
missing_ids.push id
|
128
137
|
|
129
138
|
if missing_ids.length == 0
|
130
|
-
return callback(
|
139
|
+
return callback(entries)
|
131
140
|
|
132
|
-
alert "uncached
|
141
|
+
alert "uncached entries found: #{missing_ids}"
|
133
142
|
|
134
143
|
|
135
144
|
# Saves the given object.
|
@@ -151,17 +160,16 @@ class modularity.PersistenceManager
|
|
151
160
|
diff_obj = modularity.object_diff @server_data.get(obj[@key]), obj
|
152
161
|
return if modularity.object_length(diff_obj) == 0
|
153
162
|
|
154
|
-
# Add key attribute.
|
155
|
-
diff_obj[@key] = obj[@key]
|
156
|
-
|
157
163
|
# Update server_data version.
|
158
164
|
@server_data.add obj
|
159
165
|
|
160
166
|
# Send to server
|
167
|
+
data = {}
|
168
|
+
data[@model_name] = diff_obj
|
161
169
|
jQuery.ajax
|
162
170
|
url: @entry_url(obj)
|
163
171
|
type: 'PUT'
|
164
|
-
data:
|
172
|
+
data: data
|
165
173
|
success: (server_obj) =>
|
166
174
|
@server_data.add server_obj
|
167
175
|
callback server_obj if callback
|