From 9fd365ea68fbe8318d375b778736a4294967c1d5 Mon Sep 17 00:00:00 2001 From: Alexandre Meunier Date: Wed, 29 Jul 2015 10:56:33 +0100 Subject: [PATCH] Tentative fix for #227 --- .../scripts/library/sequence-model/factory.js | 2 ++ public/scripts/sequence/models/sequences.js | 27 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/public/scripts/library/sequence-model/factory.js b/public/scripts/library/sequence-model/factory.js index be6bed41..15efecc9 100644 --- a/public/scripts/library/sequence-model/factory.js +++ b/public/scripts/library/sequence-model/factory.js @@ -1548,6 +1548,8 @@ function sequenceModelFactory(BackboneModel) { return new this.constructor(_.omit(this.toJSON(), 'id', 'history')); } + sync() {} + toJSON() { let attributes = super.toJSON(); diff --git a/public/scripts/sequence/models/sequences.js b/public/scripts/sequence/models/sequences.js index b281e3b9..6a2e63b4 100644 --- a/public/scripts/sequence/models/sequences.js +++ b/public/scripts/sequence/models/sequences.js @@ -10,12 +10,26 @@ import Backbone from 'backbone'; var constructors = {}; +var constructorFactory = function(Constructor) { + return class extends Constructor { + sync(method, model, options) { + return Backbone.getSyncMethod(model, options).apply(this, [method, model, options]); + } + static get name() { + return `${Constructor.name}WithSync`; + } + }; +}; + var SequencesCollection = Backbone.Collection.extend({ model: function(attrs, options) { - var Constructor = Sequence; + var BaseConstructor = Sequence; if(attrs._type && constructors[attrs._type]) { - Constructor = constructors[attrs._type]; + BaseConstructor = constructors[attrs._type]; } + + var Constructor = constructorFactory(BaseConstructor); + if(_.isFunction(Constructor.fromJSON)) { return Constructor.fromJSON(attrs); } else { @@ -23,6 +37,15 @@ var SequencesCollection = Backbone.Collection.extend({ } }, + add: function(models, options) { + if(_.isArray(models)) { + models = _.map(models, (model) => this.model(model.toJSON(), options)); + } else { + return this.add([models], options); + } + return Backbone.Collection.prototype.add.call(this, models, options); + }, + localStorage: new Backbone.LocalStorage('Gentle-Sequences', { serialize: function(item) { var attributes = item;