Option to prevent backbone from adding new models to a collection (update only)

Prevents upcoming/calendar from blowing up when
pull/2/head
Mark McDowall 11 years ago
parent 517b318b75
commit 9142d1b188

@ -16,7 +16,7 @@ define(
return Marionette.ItemView.extend({ return Marionette.ItemView.extend({
initialize: function () { initialize: function () {
this.collection = new CalendarCollection().bindSignalR(); this.collection = new CalendarCollection().bindSignalR({ updateOnly: true });
this.listenTo(this.collection, 'change', this._reloadCalendarEvents); this.listenTo(this.collection, 'change', this._reloadCalendarEvents);
}, },
render : function () { render : function () {

@ -11,14 +11,8 @@ define(
itemView: UpcomingItemView, itemView: UpcomingItemView,
initialize: function () { initialize: function () {
this.collection = new UpcomingCollection().bindSignalR(); this.collection = new UpcomingCollection().bindSignalR({ updateOnly: true });
this.collection.fetch(); this.collection.fetch();
this.listenTo(this.collection, 'change', this._refresh);
},
_refresh: function () {
this.render();
} }
}); });
}); });

@ -22,6 +22,8 @@ define(
this.model.set({ this.model.set({
end: end.toISOString() end: end.toISOString()
}); });
this.listenTo(this.model, 'change', this.render);
}, },
_showEpisodeDetails: function () { _showEpisodeDetails: function () {

@ -8,9 +8,10 @@ define(
], function (vent, _, Backbone) { ], function (vent, _, Backbone) {
_.extend(Backbone.Collection.prototype, { _.extend(Backbone.Collection.prototype, {
bindSignalR: function () { bindSignalR: function (bindOptions) {
var collection = this; var collection = this;
bindOptions = bindOptions || {};
var processMessage = function (options) { var processMessage = function (options) {
@ -22,6 +23,12 @@ define(
} }
var model = new collection.model(options.resource, {parse: true}); var model = new collection.model(options.resource, {parse: true});
//updateOnly will prevent the collection from adding a new item
if (bindOptions.updateOnly && !collection.get(model.get('id'))) {
return;
}
collection.add(model, {merge: true}); collection.add(model, {merge: true});
console.log(options.action + ': {0}}'.format(options.resource)); console.log(options.action + ': {0}}'.format(options.resource));
}; };

Loading…
Cancel
Save