Skip to content

Commit

Permalink
fix: fixed TypeError: Converting circular structure to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Mar 3, 2015
1 parent dddafa5 commit 11da36d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@
if(angular.isArray(data)) { // arrays are objects, we need to test for it first
return hashCode(data.toString());
} else if(angular.isObject(data)) { // constants & values for example
return hashCode(JSON.stringify(data));
return hashCode(stringify(data));
} else {
if(angular.isDefined(data) && data !== null) {
return hashCode(data.toString());
Expand Down Expand Up @@ -1016,6 +1016,22 @@
return hash;
};

var stringify = function stringify(obj) {
var cache = [];
return JSON.stringify(obj, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
});
cache = null; // Enable garbage collection
};

// Array.indexOf polyfill for IE8
if(!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
Expand Down

0 comments on commit 11da36d

Please sign in to comment.