I am using the MEAN JS framework and pretty much everything that came with it. However, I am trying to add a custom query when a user goes to a specific path (url). This is the code that came with MEAN JS: (in modules/articles/client/services)
'use strict';
//Articles service used for communicating with the articles REST endpoints
angular.module('articles').factory('Articles', ['$resource',
function ($resource) {
return $resource('api/articles/:articleId', {
articleId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
And this takes care of all the functions defined in the controller, which are: create, update, find, findOne, remove
. Now I am trying to add another function,
$scope.findByCategory = function (category) {...}
in the controller.
But in order for it to communicate with the server, I need to add the path to the services.
How do I do that? How do I edit my service (or factory) so it takes care for all the functions that I define in my controller. ( I have already set up the routes in the server side.) Said in other words, I want to combine the above code and the below code into one piece in my client service. Note: the only thing changing is the path.
//Articles service used for communicating with the articles REST endpoints
angular.module('articles').factory('Articles', ['$resource',
function ($resource) {
return $resource('api/articles/category', {
articleId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
There are multiple way to do it, you could do another factory called Category
or you can use the same returning an object like this:
angular.module('articles').factory('Articles', ['$resource',
function($resource) {
return {
articles: $resource('api/articles/:articleId', {
articleId: '@_id'
}, {
update: {
method: 'PUT'
}
}),
categories: $resource('api/articles/:articleId/category', {
articleId: '@_id'
}, {
update: {
method: 'PUT'
}
})
};
};
]);
You will call them from a controller with for example Articles.articles.get()
and Articles.categories.get()
And probably you want to change the category route to api/articles/:articleId/category
so it get categories for the articleId