基本上我有點不確定應該如何創建/管理我的資源。我正在考慮將資源看作(MVC)背景中的模型。因此,例如,這是我的工廠:角度服務/資源處理
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'}
});
});
我注意到我有一些重複任務,如插入數據。例如:
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
所以基本上我不知道如何處理類似的情況。我的工廠是否需要擴展以處理這種數據操作,還是需要爲這些類型的任務創建另一個工廠。或者這只是我過度思考這個?