-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-models.js
More file actions
176 lines (138 loc) · 4.32 KB
/
Copy pathsync-models.js
File metadata and controls
176 lines (138 loc) · 4.32 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
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _underscore = require('underscore');
var _underscore2 = _interopRequireDefault(_underscore);
var _backbone = require('backbone');
var _backbone2 = _interopRequireDefault(_backbone);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Models = {};
var remoteSync = function remoteSync(method, model, options, socket) {
var response = null;
try {
switch (method) {
case 'create':
this.clear();
this.set(model);
response = this.attributes;
break;
case 'read':
if (this instanceof Models.SyncModel) {
response = _underscore2.default.extend(this.idAttribute ? {} : { id: this.id }, this.attributes);
} else {
response = this.models.map(function (model) {
return model.attributes;
});
}
break;
case 'update':
this.clear();
this.set(model);
response = this.attributes;
break;
case 'patch':
this.set(model);
response = this.attributes;
break;
case 'delete':
this.destroy();
break;
}
} catch (error) {
response = {
name: error.name,
message: error.message
};
}
socket.emit('response', response);
};
var attachListeners = function attachListeners(socket) {
var model = this;
var modelEvents = ['create', 'read', 'update', 'patch', 'delete'];
modelEvents.forEach(function (ev) {
socket.on(ev, function (data) {
remoteSync.apply(model, data.concat(socket));
});
});
if (!this.socket) {
this.socket = socket;
}
};
var isError = function isError(obj) {
return obj && obj.name && obj.message && obj.name === 'Error';
};
var handleResponse = function handleResponse(response, model, error, options, deferred) {
var errorResponse = isError(response);
if (response && !errorResponse) {
if (options.success) {
options.success(response);
}
if (deferred) {
deferred.resolve(response);
}
} else if (error || errorResponse) {
error = error || response;
if (options.error) {
options.error(model, error, options);
}
if (deferred) {
deferred.reject(error.message);
}
}
if (options.complete) {
options.complete(response);
}
};
var sync = function sync(method, model) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var xhr = {},
deferred = _backbone2.default.$.Deferred && _backbone2.default.$.Deferred(),
handler = _underscore2.default.partial(handleResponse, _underscore2.default, model, null, options, deferred),
errorHandler = _underscore2.default.partial(handleResponse, null, model, _underscore2.default, options, deferred);
try {
model.socket.emit(method, [].concat(Array.prototype.slice.call(arguments))).once('response', handler);
} catch (syncError) {
errorHandler(syncError);
}
if (deferred) {
xhr = deferred.promise();
}
model.trigger('request', model, xhr, options);
return xhr;
};
var id = function id() {
return Math.random().toString(36).substr(2, 9);
};
Models.SyncModel = _backbone2.default.Model.extend({
sync: sync,
attachListeners: attachListeners,
initialize: function initialize() {
var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (options.socket) {
this.attachListeners(options.socket);
}
// set the id if it isn't in the attributes and doesn't yet exist
if (!attributes.idAttribute && !attributes.id && !this.id) {
this.id = id();
}
_backbone2.default.Model.prototype.initialize.apply(this, arguments);
}
});
Models.SyncModels = _backbone2.default.Collection.extend({
model: Models.SyncModel,
attachListeners: attachListeners,
sync: sync,
initialize: function initialize(models) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (options.socket) {
this.attachListeners(options.socket);
}
_backbone2.default.Collection.prototype.initialize.apply(this, arguments);
}
});
// commonjs export
module.exports = Models;
// es6 module export
exports.default = Models;