Skip to content

Instantly share code, notes, and snippets.

@andyhasit
Last active March 1, 2016 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyhasit/44cfa212045efd639bd1 to your computer and use it in GitHub Desktop.
Save andyhasit/44cfa212045efd639bd1 to your computer and use it in GitHub Desktop.
$q promise breaking loop
c = console;
angular.module('Test', []);
angular.module('Test').factory('Collection', function($q) {
var Collection = function() {var self = this;
self.__items = {};
self.__relationships = [];
};
var def = Collection.prototype;
def.deleteItem = function(item) {var self = this;
c.log('deleting ' + item._id);
var deferred = $q.defer();
var childDeletions = self.__relationships.map(function(relationship) {
return relationship.respondToItemDeleted(item, self);
});
$q.all(childDeletions).then(function() {
delete self.__items[item._id];
deferred.resolve();
});
return deferred.promise;
};
return Collection;
});
angular.module('Test').factory('ParentChildRelationship', function($q) {
var ParentChildRelationship = function(parentCollection, childCollection) {var self = this;
self.__parentCollection = parentCollection;
self.__childCollection = childCollection;
parentCollection.__relationships.push(self);
childCollection.__relationships.push(self);
self.__itemParent = {};
self.__itemChildren = {};
};
var def = ParentChildRelationship.prototype;
def.respondToItemDeleted = function (item, collection) {var self = this;
if (collection === self.__parentCollection) {
return self.__respondToParentDeleted(item);
} else if (collection === self.__childCollection) {
return self.__respondToChildDeleted(item);
}
};
def.__respondToParentDeleted = function (parentItem) {var self = this;
var children = self.__itemChildren[parentItem._id];
c.log(children.map(function(childItem){return childItem._id}));
var cascadedActionPromises = children.map(function(childItem) {
c.log('loop on ' + childItem._id);
return self.__childCollection.deleteItem(childItem);
});
c.log(cascadedActionPromises);
};
def.__respondToChildDeleted = function (childItem) {var self = this;
var parentItem = self.__itemParent[childItem._id];
//return $q.when(true); // Doesn't break loop.
if (parentItem) {
var myArray = self.__itemChildren[parentItem._id];
var index = myArray.indexOf(childItem);
if (index > -1) {
myArray.splice(index, 1);
}
}
//return $q.when(true); // DOES break loop.
delete self.__itemParent[childItem._id];
return $q.when(true); // DOES break loop.
};
return ParentChildRelationship;
});
describe('deleting', function() {
beforeEach(module('Test'));
var parentCollection,
childCollection,
relationship,
project1 = {_id: 'project1'},
task1 = {_id: 'task1'},
task2 = {_id: 'task2'},
task3 = {_id: 'task3'};
//task4 = {_id: 'task4'},
//task5 = {_id: 'task5'};
beforeEach(inject(function(_$rootScope_, Collection, ParentChildRelationship) {
$rootScope = _$rootScope_;
parentCollection = new Collection();
childCollection = new Collection();
parentCollection.items = [project1];
childCollection.items = [task1, task2, task3];
relationship = new ParentChildRelationship(parentCollection, childCollection);
relationship.__itemParent[task1._id] = project1;
relationship.__itemChildren[project1._id] = [task1, task2, task3];
}));
fit('delete cascades', function() {
parentCollection.deleteItem(project1);
$rootScope.$apply();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment