I'm refactoring my Angular app according to John Papa's style guide (well, mostly) and am in the process of making my app more modular. I've ran into a problem with route resolves. Up until this point I used a global app variable to hold my Angular app. My services also looked like this:
"use strict";
var usersService = app.service("usersService", ["$http", function ($http) {
var userService = {};
var endpoint = ClientSettings.authServiceUrl + "users";
var get = function () {
return $http.get(endpoint);
};
var getUser = function (id) {
return $http.get(endpoint + "/" + id);
};
userService.get = get;
userService.getUser = getUser;
return userService;
}]);
usersService.loadUser = ["$q", "usersService", "authService", function ($q, usersService, authService) {
var deferred = $q.defer();
usersService
.getUser(authService.authentication.id)
.success(function (user) {
deferred.resolve(user);
})
.error(function () {
deferred.reject();
});
return deferred.promise;
}];
Now when I try to refactor this, I get:
(function() {
"use strict";
angular
.module("App.Auth")
.service("usersService", usersService);
usersService.$inject = ["$http"];
function usersService($http) {
var endpoint = ClientSettings.authServiceUrl + "users";
var userService = {
get: get,
getUser: getUser
};
return userService;
var get = function () {
return $http.get(endpoint);
};
var getUser = function (id) {
return $http.get(endpoint + "/" + id);
};
}
})();
But I hit a problem with usersService.loadUser. This is a function meant to be used only for resolves like so:
.state("profile", {
url : "/profile",
controller : "profileController",
templateUrl : "profile.html",
resolve : {
user : usersService.loadUser
}
})
UsersService is a part of an Auth module, but routes are configured in the module where the app resides.
I want this module to provide a set of functions that are meant to be used for resolves. My goal is to minimize code duplication and this would certainly do a lot for that.
Aucun commentaire:
Enregistrer un commentaire