Using Angular's default router, you can resolve an AJAX resource by putting something like this in the routing:
.when('/view/:recipeId', {
controller: 'ViewCtrl',
resolve: {
recipe: ["RecipeLoader", function(RecipeLoader) {
return RecipeLoader();
}]
},
And implementing this service:
services.factory('RecipeLoader', ['Recipe', '$route', '$q',
function(Recipe, $route, $q) {
return function() {
var delay = $q.defer();
Recipe.get({id: $route.current.params.recipeId}, function(recipe) {
delay.resolve(recipe);
}, function() {
delay.reject('Unable to fetch recipe ' + $route.current.params.recipeId);
});
return delay.promise;
};
}]);
I'm working on an Ionic app at present, and have the following service:
services.factory('AlbumLoader', ['Album', '$route','$q', function (Album, $state, $q) {
return function () {
var delay = $q.defer();
Album.get({id: $route.current.params.albumId}, function (album) {
delay.resolve(album);
}, function() {
delay.reject('Unable to fetch album');
});
return delay.promise;
}
}]);
And the following route:
.state('app.album', {
cache: false,
url: "/albums/:albumId",
resolve: {
album: ['AlbumLoader', function (AlbumLoader) {
return AlbumLoader();
}]
},
views: {
'menuContent': {
templateUrl: "templates/album.html",
controller: 'AlbumCtrl'
}
}
})
Ionic uses angular-ui-router, and the documentation for it isn't exactly clear on this issue. How can I pick up the route parameters in the service using angular-ui-router in the same way the default Angular router does?
EDIT: Still having some problems with this. Using the Chrome debugger inside the loader, $state.params is an empty object when the URL is #/app/albums/17ef729c-af5b-4724-9c69-9585ab542e99. This results in the error message Error: [$resource:badcfg] Error in resource configuration for action get. Expected response to contain an object but got an array because the album ID isn't getting passed through.