“在"在资源定义中登录参数名称 [英] "at" sign in parameter names in resource definition
问题描述
来自文档(http://docs.angularjs.org/api/ngResource.$resource):
$resource(url[, paramDefaults][, actions]);
paramDefaults(optional) – {Object=}
– url 参数的默认值....如果参数值以 @
为前缀,则从数据对象中提取该参数的值.
paramDefaults(optional) – {Object=}
– Default values for url parameters.
...
If the parameter value is prefixed with @
then the value of that parameter is extracted from the data object.
问题是它们指的是什么数据对象?如何使用此功能?
The question is what data object do they refer to? How to use this feature?
推荐答案
假设您有这样的资源:
var User = $resource('/user/:userId', {userId:'@id'});
var user = User.get({userId:123});
这意味着当需要该属性时,您的 url 中 :userId
的值将替换为来自用户对象的 id
属性.
It means that the value of :userId
in your url will be replaced with the id
property from the user object when that property is required.
那么什么时候需要呢?当您对现有用户执行某些操作时需要它,例如 get
ing 一个,更新一个.创建用户时不需要.
So when is it required? Its required when you are doing something to an existing user, like get
ing one, updating one. It is not required when you create a user.
在大多数情况下,您希望在资源使用的 REST url 中至少有一个以 @ 为前缀的参数(可能是对象 ID).如果你没有,那意味着为了保存一个对象的实例,你不需要知道它的存储位置.这意味着它是一个单例对象.也许就像一个设置对象.
In most cases, you will want to have at least one param prefixed with @ in your REST url that resource uses (probably the object id). If you dont have one, that means that in order for you to save an instance of an object, you dont need to know anything about where its stored. This implies that its a singleton object. Maybe like a settings object.
这是您期待已久的示例:
Here is your long awaited example:
var User = $resource('/user/:userId/:dogName', {userId:'@id', dogName:@dog});
User.get({userId:123, dog:'Matt'}, function() { .. })
将产生请求:GET /user/123/Matt
这篇关于“在"在资源定义中登录参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!