2013-06-22 09:24:24 +03:00
|
|
|
'use strict';
|
2013-06-25 02:41:59 +03:00
|
|
|
define(
|
|
|
|
[
|
2013-10-09 04:43:41 +03:00
|
|
|
'vent',
|
2013-09-30 18:09:47 +03:00
|
|
|
'underscore',
|
|
|
|
'backbone',
|
2013-06-25 02:41:59 +03:00
|
|
|
'signalR'
|
2013-10-09 04:43:41 +03:00
|
|
|
], function (vent, _, Backbone) {
|
2013-06-25 02:41:59 +03:00
|
|
|
|
2013-08-21 22:07:28 +03:00
|
|
|
_.extend(Backbone.Collection.prototype, {
|
2013-11-30 22:40:44 +03:00
|
|
|
bindSignalR: function (bindOptions) {
|
2013-06-25 02:41:59 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
var collection = this;
|
2013-11-30 22:40:44 +03:00
|
|
|
bindOptions = bindOptions || {};
|
2013-05-06 03:33:43 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
var processMessage = function (options) {
|
2013-05-06 00:24:33 +03:00
|
|
|
|
2013-10-02 08:20:30 +03:00
|
|
|
if (options.action === 'sync') {
|
|
|
|
console.log('sync received, refetching collection');
|
|
|
|
collection.fetch();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-06 03:30:14 +03:00
|
|
|
if (options.action === 'deleted') {
|
|
|
|
collection.remove(new collection.model(options.resource, {parse: true}));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
var model = new collection.model(options.resource, {parse: true});
|
2013-11-30 22:40:44 +03:00
|
|
|
|
|
|
|
//updateOnly will prevent the collection from adding a new item
|
|
|
|
if (bindOptions.updateOnly && !collection.get(model.get('id'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
collection.add(model, {merge: true});
|
2013-09-30 18:09:47 +03:00
|
|
|
console.log(options.action + ': {0}}'.format(options.resource));
|
2013-08-21 22:07:28 +03:00
|
|
|
};
|
|
|
|
|
2013-10-09 04:43:41 +03:00
|
|
|
collection.listenTo(vent, 'server:' + collection.url.replace('/api/', ''), processMessage);
|
2013-08-10 21:38:01 +03:00
|
|
|
|
2013-08-21 22:07:28 +03:00
|
|
|
return this;
|
|
|
|
},
|
2013-08-10 21:38:01 +03:00
|
|
|
|
2013-08-21 22:07:28 +03:00
|
|
|
unbindSignalR: function () {
|
2013-08-10 21:38:01 +03:00
|
|
|
|
2013-08-21 22:07:28 +03:00
|
|
|
}});
|
2013-06-25 02:41:59 +03:00
|
|
|
});
|
2013-05-06 03:33:43 +03:00
|
|
|
|
|
|
|
|