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

Prevents upcoming/calendar from blowing up when
This commit is contained in:
Mark McDowall 2013-11-30 11:40:44 -08:00
parent 517b318b75
commit 9142d1b188
4 changed files with 12 additions and 9 deletions

View File

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

View File

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

View File

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

View File

@ -8,9 +8,10 @@ define(
], function (vent, _, Backbone) {
_.extend(Backbone.Collection.prototype, {
bindSignalR: function () {
bindSignalR: function (bindOptions) {
var collection = this;
bindOptions = bindOptions || {};
var processMessage = function (options) {
@ -22,6 +23,12 @@ define(
}
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});
console.log(options.action + ': {0}}'.format(options.resource));
};