I have a service that can handle four different types of http requests, but all return data following the same model structure. I am looking for advice on how to save the results from the service along with some specific data in my JavaScript Angular code.
Currently, I have implemented it by creating a factory that returns a new instance of a class storing the results and specific data.
app.factory('myFactory', function () {
var myDataModel = function (name) {
var self = this;
self.name = name;
self.results = [];
self.isSearching = false;
self.get = function (id) {
self.isSearching = true;
$http.get('url' + '?name=' + self.name + '&id=' + id)
.then(function (data) {
self.results = data;
self.isSearching = false;
});
};
self.display = function () {
alert('the result of ' + self.id + ' is:' + self.results.join(', '));
};
};
return {
myDataModel: function () {
return new myDataModel();
}
};
});
I'm unsure if this approach is the best way to do it. I would appreciate suggestions on other implementation methods like using a service or directive instead.
Thank you!
kfir.