I'm working on a AngularJS application, I'm new to angular, and I'm setting a big object in the $rootScope to be available from the $scope whenever I need it.
At this point it looks like this :
Application.run(['$rootScope', function($rootScope) {
$rootScope.Envato = {
API : {
Path : {
Private : "/api/private/",
Public : "/api/public/"
},
Methods : {
Private : {
User : {
Vitals : $rootScope.Envato.API.Path.Private + "get-user-vitals.php",
Transactions : $rootScope.Envato.API.Path.Private + "get-earnings-and-sales-by-month.php",
Statement : $rootScope.Envato.API.Path.Private + "get-statement.php",
Sales : $rootScope.Envato.API.Path.Private + "get-recent-sales.php",
Account : $rootScope.Envato.API.Path.Private + "get-account-information.php"
},
Item : {
Purchase : {
Information : $rootScope.Envato.API.Path.Private + "get-purchase-information.php",
Download : $rootScope.Envato.API.Path.Private + "get-item-download-url.php"
}
}
},
Public : {
API : {
Releases : $rootScope.Envato.API.Path.Public + "get-api-releases.php"
},
Blog : {
Posts : $rootScope.Envato.API.Path.Public + "get-blog-posts.php",
Threads : {
Active : $rootScope.Envato.API.Path.Public + "get-active-threads.php",
Status : $rootScope.Envato.API.Path.Public + "get-thread-status.php"
},
},
Collections : $rootScope.Envato.API.Path.Public + "get-collections.php",
Items : {
Count : $rootScope.Envato.API.Path.Public + "get-number-of-items.php",
Popular : $rootScope.Envato.API.Path.Public + "get-popular-items.php",
Featured : $rootScope.Envato.API.Path.Public + "get-featured-items.php",
New : {
Marketplace : $rootScope.Envato.API.Path.Public + "get-new-items-from-market.php",
User : $rootScope.Envato.API.Path.Public + "get-new-items-from-user.php",
Random : $rootScope.Envato.API.Path.Public + "get-random-new-items.php"
},
Information : $rootScope.Envato.API.Path.Public + "get-item-information.php",
Prices : $rootScope.Envato.API.Path.Public + "get-item-prices.php"
},
Users : {
Total : $rootScope.Envato.API.Path.Public + "get-total-users-count.php",
Information : $rootScope.Envato.API.Path.Public + "get-user-information.php",
Items : {
Count : $rootScope.Envato.API.Path.Public + "get-user-items-by-site.php"
}
},
Search : $rootScope.Envato.API.Path.Public + "get-search-results.php"
}
}
}
};
$rootScope.$safeApply = function($scope, fn) {
fn = fn || function() {};
if($scope.$$phase) {
fn();
} else {
$scope.$apply(fn);
};
};
}]);
But this is a mess and I was wondering if there is a better way of doing it, or maybe AngularJS has some other awesome stuff that can help me do something similar :)
EDIT : Obviously, something like $rootScope.Envato.API.Path.Private will not work, but I added it as it would be easier to understand what is it that I'm trying to do.
It looks like you're looking for a service. Check out this guide on the AngularJS site to learn how to create them.
Application.factory('Envato', function() {
return {
API: {
Path: { ... },
}
};
});
Application.controller('SomeController', function($scope, Envato) {
$scope.thing = Envato.API...;
});