What I'm trying to do is to resolve the item I need using a loop rather than create a API request for that item due to the fact I already have that item in memory.
So, I have a list if items in my service and when I click to go to the item details I do this:
.state('app.itemDetails', {
url: '/items/:itemId',
templateUrl: 'modules/items/views/items.details.view.html',
controller: 'DetailsController',
resolve: angular.extend(
helper.resolveFor('ngDialog'), {
item: function ($q, $stateParams, itemsManager) {
var deferred = $q.defer();
var obj = {};
var promise = _.each(itemsManager.list, function (item) {
if (item.id === $stateParams.itemId) {
obj = item;
}
return $q.when(obj);
});
$q.all(promise).then(function (item) {
deferred.resolve(item);
});
return deferred.promise;
}
}
)
})
It looks like it bypass the _.each and return always the same item...
How can I handle it?
EDIT:
// Generates a resolve object by passing script names
// previously configured in constant.APP_REQUIRES
this.resolveFor = function () {
var _args = arguments;
return {
deps: ['$ocLazyLoad','$q', function ($ocLL, $q) {
// Creates a promise chain for each argument
var promise = $q.when(1); // empty promise
for(var i=0, len=_args.length; i < len; i ++){
promise = andThen(_args[i]);
}
return promise;
// creates promise to chain dynamically
function andThen(_arg) {
// also support a function that returns a promise
if(typeof _arg == 'function')
return promise.then(_arg);
else
return promise.then(function() {
// if is a module, pass the name. If not, pass the array
var whatToLoad = getRequired(_arg);
// simple error check
if(!whatToLoad) return $.error('Route resolve: Bad resource name [' + _arg + ']');
// finally, return a promise
return $ocLL.load( whatToLoad );
});
}
// check and returns required data
// analyze module items with the form [name: '', files: []]
// and also simple array of script files (for not angular js)
function getRequired(name) {
if (appRequires.modules)
for(var m in appRequires.modules)
if(appRequires.modules[m].name && appRequires.modules[m].name === name)
return appRequires.modules[m];
return appRequires.scripts && appRequires.scripts[name];
}
}]};
}; // resolveFor
EDIT 2:
var id = $stateParams.itemId;
return _.each(ItemsManager.list, function (item) {
if (item.id == id) {
console.log('found');
console.log(item);
return item;
}
});
In the console.log I receive the item correctly. The view do not receive it...