Reputation:
I have a project resource that consists of multiple task resource (similar to Facebook post and comments relationship)
When I update a project, I want to use url /project/[project id]
. and when I update a task, I want to use url /project/[project id]/[task id]
Here is my project resource service:
angular.module('project').factory('Project', ['$resource', function($resource) {
return $resource('project/:projectId', {
projectId: '@_id'
}, {
update: {
method: 'PUT'
}
})
}])
Now I wanna define the task resource:
angular.module('task').factory('Task', ['$resource', function($resource) {
return $resource('project/:projectId/:taskId', {
projectId: '' //how can i pass in the project Id from controller?
taskId: '@_id'
}, {
update: {
method: 'PUT'
}
})
}])
In my controller, if i just update project, not its task, then I simply use:
$scope.update = function() {
$scope.project.$update(function() {
$location.path('project/' + $scope.project._id)
}, function(errResponse) {
$scope.error = errorResponse.data.message
})
}
But when I update the tasks of the project, I wanna pass in the project to construct the url. How can I do that?
Notice that project and task belong to separate modules. user can update the task in project detail page, or in task detail page.
Upvotes: 3
Views: 2721
Reputation: 2790
angular.module('task').factory('$task',function($resource){
return $resource('project/:id/:taskid',
{
id: '@id',
taskid: '@taskid'
},{
'update': {method: 'PUT'},
'list': {method: 'GET'},
'create':{method: 'POST'},
'delete': {method: 'DELETE'}
});
});
$task.list({
id : 1, // json prop name should be same as declared in factory
taskid : 10
});
This will send ajax request to : /project/1/10
If you want to update task you have to give its project id.
Upvotes: 5
Reputation: 19569
You pass it the same way you do other named params:
angular.module('task').factory('Task', ['$resource', function($resource) {
return $resource('project/:projectId/:taskId', {
projectId: '@pid',
taskId: '@_id'
}, {
update: {
method: 'PUT'
}
});
Your data model will then have _id
and pid
properties which ng will track, ie.:
this.projectId = 17 // or however you set it.
var task = new Task({
_id: '213',
pid: this.projectId
});
task.save(); // POST /project/17/213
See? So the second param to the $resource is params. The ones you map to your URL will go to URL, the others in request body (or query string, depends).
Upvotes: 0
Reputation: 44
Im not sure what ur doing.
Its smarter to make first a "get" with the project module for the special one project.
If u have this Object, u can update it.
This looks like this:
angular.module('project').factory('Project', ['$resource', function($resource) {
return $resource('project/:projectId', {
projectId: '@_id'
}, {
update: {
method: 'PUT'
},
get: {
method:'GET'
}
})
}])
Project.get({projectId: projectId}, function(response){
//the response contains the data of the project Id
//I cant test it now, ....check the docu.
//now u could update and take the id still from the response
})
Upvotes: 0