Skip to content

Commit

Permalink
Tentative fix for #227
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandremeunier committed Jul 29, 2015
1 parent 098f1fe commit 9fd365e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions public/scripts/library/sequence-model/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,8 @@ function sequenceModelFactory(BackboneModel) {
return new this.constructor(_.omit(this.toJSON(), 'id', 'history'));
}

sync() {}

toJSON() {

let attributes = super.toJSON();
Expand Down
27 changes: 25 additions & 2 deletions public/scripts/sequence/models/sequences.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,42 @@ 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 {
return new Constructor(attrs, options);
}
},

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;
Expand Down

0 comments on commit 9fd365e

Please sign in to comment.