/Angular-Transaction-manager

track the history of a $scope object, do rollback, go to prev version etc...

Primary LanguageApacheConfMIT LicenseMIT

Angular Transaction Manager

Angularjs Module that allows to make snapshots of an object, perform a rollback and restore snapshot version.

Install

Bower

bower install angular-transaction-manager

include transactionManager.js in your application

 <script src="scripts/transactionManager.js"></script>

add transactionManager to your angular module

var app = angular.module('myApp', ['transactionManager']);

try demo

Live demo!

or Open example folder and run

 npm install
 grunt install
 bower install

run grunt server in example folder

API

works only with $scope object (not with primitives) like this

$scope.foo = {};

TransactionManager.snapshot save the actual state of passed object
$scope.foo = {a: 1, b: "test"};
TransactionManager.snapshot($scope.foo);

the result object contains a key snapshot

{a: 1, b: "test", snapshot: [{a: 1, b: "test"}]}

you can call snapshot() more than 1 time, the result object will contain a list of all snapshot

$scope.foo = {a: 1, b: "test"};
TransactionManager.snapshot($scope.foo);
$scope.foo.b = "changed";
TransactionManager.snapshot($scope.foo);
// $scope.foo will be:
//{a: 1, b: "test", snapshot: [{a: 1, b: "test"}, {a: 1, b: "changed"}]}

TransactionManager.rollback applies the last snapshot
$scope.foo = {a: 1, b: "test"};
TransactionManager.snapshot($scope.foo);
$scope.foo.b = "test2"; // now foo is ->  {a: 1, b: "test2"}
TransactionManager.rollback($scope.foo);
// now foo is ->  {a: 1, b: "test"}

TransactionManager.canRollback returns true in case it has a snapshot and actual state is different from last snapshot
TransactionManager.canRollback($scope.foo);

TransactionManager.canRestorePrevious return true if passed object has a previous snapshot (old TransactionManager.prevVersion)
TransactionManager.canRestorePrevious($scope.foo);

TransactionManager.restorePrevious Restore to the state of previous snapshot version.
$scope.foo = {a: 1, b: "test"};
TransactionManager.snapshot($scope.foo); // now foo is -> {a: 1, b: "test"}
$scope.foo.b = "test2";
TransactionManager.snapshot($scope.foo); // now foo is -> {a: 1, b: "test2"}
TransactionManager.restorePrevious($scope.foo);
// now foo is ->  {a: 1, b: "test"}

TransactionManager.clear Remove all snapshots
TransactionManager.clear($scope.foo);

TransactionManager.hasSnapshot returns true if passed object has at least 1 snapshot
TransactionManager.hasSnapshot($scope.foo);