This repository is currently being migrated. It's locked while the migration is in progress.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
230 lines (185 loc) · 6.1 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
define([
'underscore',
'backbone',
'component',
'intent'
], function (_, Backbone, Component, Intent) {
'use strict';
var Store = Component.extend();
Store.extend = function () {
return Object.create(Store);
};
Store.init = function (intent) {
Component.init.call(this, intent);
// Collection containing all models
this.loaded = new this.Collection();
this.loading = {};
};
/**
*
*/
Store.load = function (intent) {
if (this.DEBUG) console.info('STORE load', intent);
if (_.isArray(intent.data)) {
// Multiple items
var models = [];
_.each(intent.data, function (itemData) {
models.push(this._add(new this.Model(itemData)));
}, this);
intent.respond(null, models);
} else if (_.isObject(intent.data)) {
// Single item
intent.respond(null, this._add(new this.Model(intent.data)));
} else {
intent.respond(new TypeError('Object or array required'));
}
};
/**
* Responds to intent with wanted model(s)
*
* @todo Doens't keep order when requesting multiple models
*/
Store.get = function (intent) {
if (this.DEBUG || intent.DEBUG) console.info('STORE get', intent);
var inst = this;
if (_.isArray(intent.data)) {
var length = intent.data.length,
models = new this.Collection();
if (length > 0) {
_.each(intent.data, function (id) {
inst._get(id, function (err, model) {
if (err) {
intent.respond(err);
} else {
models.add(model);
if (models.length == length) {
intent.respond(null, models);
}
}
});
});
} else {
// Respond to empty array with empty collection
intent.respond(null, models);
}
} else {
this._get(intent.data, intent.respond);
}
};
Store.edit = function (intent) {
var inst = this;
this._get(intent.data, function (err, model) {
if (err) return intent.respond(err);
intent.respond(null, model, function () {
inst._save(model, function (err, model) {
});
});
});
}
/**
* Requests models from server. Responds to intent with Collection
* containing models and total count of models
*
* @param {Intent} intent
* @param {Object} intent.data Request data
*/
Store.find = function (intent) {
var inst = this,
col = new this.Collection();
col.fetch({
data: intent.data,
success: function (collection, response) {
var responseCol = new inst.Collection();
collection.each(function (model) {
responseCol.add(inst._add(model));
});
intent.respond(null, responseCol, response.count);
},
error: function () {
var err = new Error("Couldn't find resources");
intent.respond(err);
}
});
};
Store.save = function (intent) {
var inst = this,
data = intent.data;
if (data.id) {
this._get(data.id, function (err, model) {
if (!err) {
model.set(data);
inst._save(model, intent.respond);
} else {
intent.respond(err, model);
}
});
} else {
var model = new this.Model(intent.data);
inst._save(model, function (err, model) {
if (!err) {
model = inst._add(model);
}
intent.respond(err, model);
});
}
};
Store._save = function (model, callback) {
var inst = this;
model.save(null, {
success: function () {
if (callback) callback.call(inst, null, model);
inst.afterSave(model);
Intent.create(inst.uid + ':changed', model.id).send();
},
error: function () {
var err = new Error('Save failed');
if (callback) callback.call(inst, err, model);
}
});
};
Store.afterSave = function (model) {};
Store._get = function (id, callback) {
var inst = this,
model = this.loaded.get(id);
if (model) {
// Model is already loaded
callback(null, model);
} else if (this.loading[id]) {
// Model is already being requested
// Push callback to queue
this.loading[id].push(callback);
} else {
this.loading[id] = [callback];
model = new this.Model();
model.id = id;
model.bind('change', function() {
model.unbind('change');
inst._add(model);
});
model.fetch();
}
};
/**
* Adds model to loaded list of models and calls queued callbacks
*
*/
Store._add = function (newModel) {
var oldModel = this.loaded.get(newModel.id);
if (!oldModel) {
this.loaded.add(newModel);
if (this.loading[newModel.id]) {
while (this.loading[newModel.id].length > 0) {
var storedCallback = this.loading[newModel.id].shift();
storedCallback(null, newModel);
}
}
return newModel;
} else {
return oldModel;
}
};
Store._triggerChanged = function (model) {
Intent.create(this.uid + ':changed', model.id).send();
};
return Store;
});