Reputation: 2443
Basically I am a little bit unsure how should I create/manage my resource. I am thinking about resource as a model like in (MVC) background. So for example this is my factory:
angular.module('resources.question', ['ngResource'])
.factory('Question', function ($resource) {
return $resource('/:questionId', {questionId: '@id'}, {
postTest: {method: 'POST', url: '/create/:pageId/:questionId', params: {questionId: 0}},
search: {method: 'GET', url: '/search/:query', params: {query: ''}, isArray: true},
edit: {method: 'GET', url: '/edit/:pageQuestionId'},
delete: {method: 'GET', url: '/delete/:pageQuestionId'},
addExisting: {method: 'GET', url: '/addtopage/:pageId/:questionId'}
});
});
I am noticing that I have some repeating tasks, like inserting data. For example:
var newQuestion = Question.addExisting({
pageId: data.pageId,
questionId: data.questionId,
id: $scope.data.search.question.id
});
//update object from database
$rootScope.survey.pages[data.pageIndex].questions.splice(data.questionIndex, 0, newQuestion); //insert the data
So basically I am not sure how to handle similar situations. Does my factory needs to extended somehow to handle this kind of data manipulation, or do I need to create another factory for these kind of tasks. Or it's just me over-thinking this ?
Upvotes: 1
Views: 151
Reputation: 160
This isn't a direct answer to your question, but be careful with your custom $resource action names. Using delete
could cause problems if you try to minify your code, as it is a reserved word in Javascript. Its generally considered bad practice to use reserved words as property names.
with insight from here:
Upvotes: 0
Reputation: 3124
Why don't you just abstract away all the repeated code into another service and inject that into your controller or wherever you are using the current resource?
Setup something like a questionManagement service where you inject your current question service and create an API where you work on the question domain at a higher level. Put all the things that you do after a response to the resource comes back in those various methods.
Remember since all this will be async that you will either need to implement promises via the $q service or a callback function.
Upvotes: 1