I am encountering an issue with altering the folders variable (an array) within the return statement of my Angular factory. It seems to work for the create action, but I am having trouble with the scope of the variable in the factory.
This code works:
folders.push(response.folder)
However, when I use the underscore.js function _without (which returns an array), it does not modify the folder variable as expected.
folders = _.without(folders, _.findWhere(folders, {id: folder.id }))
Below is the snippet of my factory code:
angular.module('cmsApp')
.factory('Folders', function(Restangular){
var folders = Restangular.all('folders').getList().$object;
return {
get: function(){
return folders;
},
create: function(folderName) {
folder = { label: folderName };
folders.post(folder).then(function(response) {
folders.push(response.folder);
})
return folders;
},
destroy: function(folder) {
folders.remove(folder).then(function(){
folders = _.without(folders, _.findWhere(folders, {id: folder.id }))
})
return folders;
}
}
});
The create function successfully updates the folders variable, whereas the destroy function does not seem to affect the folders variable as intended.