Skip to content

Commit

Permalink
localStorage implementation tweaks
Browse files Browse the repository at this point in the history
- For consistency, uses of localStorage not being called via $window have been updated to do so.
- To allow for potentiall fallbacks / polyfilling* of localStorage functionality the implementation has been moved away from accessing data directly through keys to the Storage objects functions.

* For example, Safari's private browsing mode which has a 0 size quota on storage.
  • Loading branch information
atomworks committed Jan 29, 2016
1 parent 711778c commit de5c34b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions dist/ngCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ angular.module('ngCart', ['ngCart.directives'])

}])

.service('ngCart', ['$rootScope', 'ngCartItem', 'store', function ($rootScope, ngCartItem, store) {
.service('ngCart', ['$rootScope', '$window', 'ngCartItem', 'store', function ($rootScope, $window, ngCartItem, store) {

this.init = function(){
this.$cart = {
Expand Down Expand Up @@ -152,7 +152,7 @@ angular.module('ngCart', ['ngCart.directives'])

$rootScope.$broadcast('ngCart:change', {});
this.$cart.items = [];
localStorage.removeItem('cart');
$window.localStorage.removeItem('cart');
};

this.isEmpty = function () {
Expand Down Expand Up @@ -307,8 +307,8 @@ angular.module('ngCart', ['ngCart.directives'])
return {

get: function (key) {
if ($window.localStorage [key]) {
var cart = angular.fromJson($window.localStorage [key]);
if ( $window.localStorage.getItem(key) ) {
var cart = angular.fromJson( $window.localStorage.getItem(key) ) ;
return JSON.parse(cart);
}
return false;
Expand All @@ -319,11 +319,11 @@ angular.module('ngCart', ['ngCart.directives'])
set: function (key, val) {

if (val === undefined) {
$window.localStorage .removeItem(key);
$window.localStorage.removeItem(key);
} else {
$window.localStorage [key] = angular.toJson(val);
$window.localStorage.setItem( key, angular.toJson(val) );
}
return $window.localStorage [key];
return $window.localStorage.getItem(key);
}
}
}])
Expand Down
2 changes: 1 addition & 1 deletion dist/ngCart.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions src/ngCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ angular.module('ngCart', ['ngCart.directives'])

}])

.service('ngCart', ['$rootScope', 'ngCartItem', 'store', function ($rootScope, ngCartItem, store) {
.service('ngCart', ['$rootScope', '$window', 'ngCartItem', 'store', function ($rootScope, $window, ngCartItem, store) {

this.init = function(){
this.$cart = {
Expand Down Expand Up @@ -152,7 +152,7 @@ angular.module('ngCart', ['ngCart.directives'])

$rootScope.$broadcast('ngCart:change', {});
this.$cart.items = [];
localStorage.removeItem('cart');
$window.localStorage.removeItem('cart');
};

this.isEmpty = function () {
Expand Down Expand Up @@ -307,8 +307,8 @@ angular.module('ngCart', ['ngCart.directives'])
return {

get: function (key) {
if ($window.localStorage [key]) {
var cart = angular.fromJson($window.localStorage [key]);
if ( $window.localStorage.getItem(key) ) {
var cart = angular.fromJson( $window.localStorage.getItem(key) ) ;
return JSON.parse(cart);
}
return false;
Expand All @@ -319,11 +319,11 @@ angular.module('ngCart', ['ngCart.directives'])
set: function (key, val) {

if (val === undefined) {
$window.localStorage .removeItem(key);
$window.localStorage.removeItem(key);
} else {
$window.localStorage [key] = angular.toJson(val);
$window.localStorage.setItem( key, angular.toJson(val) );
}
return $window.localStorage [key];
return $window.localStorage.getItem(key);
}
}
}])
Expand Down

0 comments on commit de5c34b

Please sign in to comment.