From 70acf039d6c49b703c795d4e71b88dbdbdfdfe09 Mon Sep 17 00:00:00 2001 From: Cody Nguyen Date: Mon, 15 Aug 2016 11:05:58 +0700 Subject: [PATCH] add keepState option to .restore() --- backbone.memento.js | 11 ++++++++--- readme.md | 20 +++++++++++--------- spec/javascripts/memento.spec.js | 17 ++++++++++++++++- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/backbone.memento.js b/backbone.memento.js index 2be289f..e793cd7 100644 --- a/backbone.memento.js +++ b/backbone.memento.js @@ -31,7 +31,12 @@ Backbone.Memento = (function(Backbone, _){ }; this.restore = function(restoreConfig){ - var previousState = mementoStack.pop(); + var previousState; + if (restoreConfig && restoreConfig.keepState) { + previousState = _.last(mementoStack); + } else { + previousState = mementoStack.pop(); + } restoreState(previousState, restoreConfig); }; @@ -124,7 +129,7 @@ Backbone.Memento = (function(Backbone, _){ restoreConfig = _.extend({}, config, restoreConfig); restoreState(previousState, restoreConfig); } - + }; // ---------------------------- @@ -140,7 +145,7 @@ Backbone.Memento = (function(Backbone, _){ this.push = function(attrs){ attributeStack.push(attrs); } - + this.pop = function(restoreConfig){ var oldAttrs = attributeStack.pop(); return oldAttrs; diff --git a/readme.md b/readme.md index 5b643e0..c48505f 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,7 @@ any round trips. ## Getting Started -It's easy to get up and running. You only need to have Backbone (including underscore.js - +It's easy to get up and running. You only need to have Backbone (including underscore.js - a requirement for Backbone) in your page before including the backbone.memento plugin. @@ -26,7 +26,7 @@ Namely, it uses the `set` and `unset` methods of models and `reset` and `remove` ### Get The Memento Plugin -Download the `backbone.memento.js` file from this github repository and copy it into +Download the `backbone.memento.js` file from this github repository and copy it into your javascripts folder. Add the needed `