-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.js
More file actions
69 lines (66 loc) · 1.66 KB
/
Copy pathrequests.js
File metadata and controls
69 lines (66 loc) · 1.66 KB
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
function request(action, callback) {
$.ajax({
data: {action: JSON.stringify(action)},
url: 'server/submit.php',
type: 'POST',
success: function(data) {
callback(JSON.parse(data));
}
});
}
var Store = function() {
this.read = function(model, success) {
// fetch single, otherwise fetch all
if (model.id)
request({command: 'fetch', table: model.table, id: model.id},
function(attributes){
$.each(attributes, function(attr, val){
model.set(attr, val);
});
success(model);
});
else
request({
command: 'fetchAll',
table: model.table,
params: model.params
},
function(data){
var items = [];
$.each(data, function(id, itemData){
var item = new model.model();
$.each(itemData, function(attr, val){
if (isNaN(parseInt(attr)) && attr != 'id') {
var args = {};
args[attr] = val;
item.set(args);
}
});
item.id = id;
items.push(item);
});
success(items);
});
}
this.create = function(model, success) {
request({command: 'add', table: model.table, attributes: model.toJSON()},
function(data){
model.id = data.insertId;
success(model);
});
}
this.update = function(model, success) {
request({command: 'update', table: model.table, id: model.id, attributes: model.toJSON()}, function(data){
success(model);
});
}
this['delete'] = function(model, success) {
request({command: 'delete', table: model.table, id: model.id}, function(data){
success(model);
});
}
}
window.store = new Store();
Backbone.sync = function(method, model, options) {
var resp = window.store[method](model, options.success);
};