I am very new to the javascript scene and especially Angular. I have a question about $http calls to a joomla server that has very very basic REST services using K2's "format=json"
url extension. The way the K2 joomla json output works is that you just append "format=json"
to the end of your url: https://examplesite.com/category-1?format=json
now this works fine but I cant help thinking I am doing something wrong.. If I want a page that shows articles from category-1 and another that shows articles from category-2 then 100 more categories after that, I am making a seperate $http call every time:
angular.module('TNA.servicesEastc', [])
.factory('Eastc', function ($http) {
var eastc = [];
storageKey = "eastc";
function _getCache() {
var cache = localStorage.getItem(storageKey );
if (cache)
eastc = angular.fromJson(cache);
}
return {
all: function () {
return $http.get("http://examplesite.com/category-1.html?format=json").then(function (response) {
eastc = response.data.items;
console.log(response.data.items);
return eastc;
});
},
getNew: function () {
return $http.get("http://examplesite.com/category-1.html?format=json").then(function (response) {
eastc = response.data.items;
return eastc;
});
},
get: function (eastCId) {
if (!eastc.length)
_getCache();
for (var i = 0; i < eastc.length; i++) {
if (parseInt(eastc[i].id) === parseInt(eastCId)) {
return eastc[i];
}
}
return null;
}
}
});
and another...
angular.module('TNA.servicesWestc', [])
.factory('Westc', function ($http) {
var westc = [];
storageKey = "westc";
function _getCache() {
var cache = localStorage.getItem(storageKey );
if (cache)
westc = angular.fromJson(cache);
}
return {
all: function () {
return $http.get("http://examplesite.com/category-2.html?format=json").then(function (response) {
westc = response.data.items;
console.log(response.data.items);
return westc;
});
},
getNew: function () {
return $http.get("http://examplesite.com/category-2.html?format=json").then(function (response) {
westc = response.data.items;
return westc;
});
},
get: function (westCId) {
if (!westc.length)
_getCache();
for (var i = 0; i < westc.length; i++) {
if (parseInt(westc[i].id) === parseInt(westCId)) {
return westc[i];
}
}
return null;
}
}
});
and so on...
its the same problem with having to make tons of controllers to handle all this.
This doesn't seem very maintainable to me so I was wondering if there is a more efficient way to handle this?