One thing I'm trying to figure out is how to utilize a service like $http outside of the $get function. Is this even possible? Currently, my code loads a json file that contains a dictionary used by my application in various ways. I want users to have the ability to customize this dictionary, so I'm using jQuery's extend method to allow them to add values and extend the dictionary. The instantiate method within the following code handles all of this. My goal is to configure my service as follows:
config(['_sys_dictionaryProvider', function(_sys_dictionaryProvider) {
_sys_dictionaryProvider.instansiate('config/dictionary/custom/dictionary.json');
}])
However, this setup requires the $http service to be accessible during configuration, which I believe is not the case. If I include the $http service as part of the $get property, it will work (as mentioned here), but this means the network needs to be queried every time the service is used. Is there any way to use a service in the configuration of another service?
Below is the full code, please let me know if further clarification is needed.
app.provider("_sys_dictionary", ['$http',
function ($http) {
var dictionary,
DictionaryService = function () {
this.definitions = dictionary;
this.define = function (what) {
var definitions = this.definitions;
if (what instanceof Array) {
for (var i = 0; i < what.length; i++) {
definitions = definitions[what[i]];
}
return definitions;
}
return this.definitions[what];
};
};
return {
$get: function () {
return new DictionaryService();
},
instansiate: function (path) {
$http.get('config/dictionary/dictionary.json').success(function (data) {
dictionary = data;
$http.get(path).success(function (data) {
jQuery.extend(true, dictionary, data)
});
});
}
};
}
]);