Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 4da71f3

Browse files
committed
Merge pull request #197 from chlu/fix-autosave
midgardStorage: Fix inconsistent auto-save behavior
2 parents d2d443a + eeb0b52 commit 4da71f3

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

dist/create.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,9 @@ See http://createjs.org for more information
18381838
widget.element.on(widget.options.editableNs + 'changed', function (event, options) {
18391839
if (_.indexOf(widget.changedModels, options.instance) === -1) {
18401840
widget.changedModels.push(options.instance);
1841+
options.instance.midgardStorageVersion = 1;
1842+
} else {
1843+
options.instance.midgardStorageVersion++;
18411844
}
18421845
widget._saveLocal(options.instance);
18431846
});
@@ -2002,15 +2005,18 @@ See http://createjs.org for more information
20022005
options: options
20032006
});
20042007

2005-
var widget = this;
2008+
var widget = this,
2009+
previousVersion = model.midgardStorageVersion;
20062010
model.save(null, _.extend({}, options, {
20072011
success: function (m, response) {
20082012
// From now on we're going with the values we have on server
20092013
model._originalAttributes = _.clone(model.attributes);
20102014
widget._removeLocal(model);
20112015
window.setTimeout(function () {
2012-
// Remove the model from the list of changed models after saving
2013-
widget.changedModels.splice(widget.changedModels.indexOf(model), 1);
2016+
// Remove the model from the list of changed models after saving if no other change was made to the model
2017+
if (model.midgardStorageVersion == previousVersion) {
2018+
widget.changedModels.splice(widget.changedModels.indexOf(model), 1);
2019+
}
20142020
}, 0);
20152021
if (_.isFunction(options.success)) {
20162022
options.success(m, response);

src/jquery.Midgard.midgardStorage.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
widget.element.on(widget.options.editableNs + 'changed', function (event, options) {
118118
if (_.indexOf(widget.changedModels, options.instance) === -1) {
119119
widget.changedModels.push(options.instance);
120+
options.instance.midgardStorageVersion = 1;
121+
} else {
122+
options.instance.midgardStorageVersion++;
120123
}
121124
widget._saveLocal(options.instance);
122125
});
@@ -281,15 +284,18 @@
281284
options: options
282285
});
283286

284-
var widget = this;
287+
var widget = this,
288+
previousVersion = model.midgardStorageVersion;
285289
model.save(null, _.extend({}, options, {
286290
success: function (m, response) {
287291
// From now on we're going with the values we have on server
288292
model._originalAttributes = _.clone(model.attributes);
289293
widget._removeLocal(model);
290294
window.setTimeout(function () {
291-
// Remove the model from the list of changed models after saving
292-
widget.changedModels.splice(widget.changedModels.indexOf(model), 1);
295+
// Remove the model from the list of changed models after saving if no other change was made to the model
296+
if (model.midgardStorageVersion == previousVersion) {
297+
widget.changedModels.splice(widget.changedModels.indexOf(model), 1);
298+
}
293299
}, 0);
294300
if (_.isFunction(options.success)) {
295301
options.success(m, response);

0 commit comments

Comments
 (0)