angular.module('clientApp')
.factory('User', ['$resource', 'UserCard',
function ($resource, UserCard) {
var User = $resource(
"user/:id",
{id: "@id" },
{
'save': {method: 'PUT'},
'add': {method: 'POST'}
}
);
User.prototype.getCards = function (params, callback) {
UserCard.prototype.userId = this.id;
params.userId = this.id;
return UserCard.query(params, callback);
};
return User;
}])
.factory('UserCard', ['$resource',
function ($resource) {
return $resource(
"user/:userId/card/:id",
{userId: '@userId', id: "@id" },
{
'save': {method: 'PUT'},
'add': {method: 'POST'}
}
);
}])
.controller('TestCtrl', ['User',
function (User) {
// GET user/123
var user = User.get({id: 123}, function () {
// GET user/123/card/321
var card = user.getCard({id: 321}, function () {
card.someAttribute = 'Some value';
// PUT user/123/card/321
card.$save();
});
});
}])
;
检查我的代码。这样我就得到了已经与用户关联的卡片,因此 card.$save() 或 card.$query() 不需要传递 userId 或 at 参数,也不需要传递 card 的属性。