我正在访问一个 REST 服务,它公开了这两个资源,一个父资源和一个子资源:
/users
/users/{userId}/account
所以资源“帐户”没有嵌套在资源“用户”中,它必须被第二个请求访问。有此类 REST API 的示例,例如这里
我使用这些模型将用户及其帐户映射到 Ext Js 4 数据模型:
用户
Ext.define("MyApp.model.User", {
extend: "Ext.data.Model",
fields: [ { name: "id", type: "string" }],
associations: [{
model: "MyApp.model.Account",
name: "account",
type: "hasOne",
reader: "json",
getterName: "getAccount",
setterName: "setAccount",
foreignKey: "accountId"
}
],
proxy: {
type: "rest",
url: "/rest/users",
reader: {
type: "json",
totalProperty: "total",
root: "users"
}
}
});
帐户
Ext.define("MyApp.model.Account", {
extend: "Ext.data.Model",
fields: [ { name: "id", type: "string" }],
belongsTo: "MyApp.model.User",
proxy: {
type: "rest",
reader: { type: "json"}
}
});
帐户代理没有 url(我希望这将基于父用户模型创建)。当我调用 user.getAccount() 时,我得到一个异常,因为代理缺少 url。
问题:是否有某种方法可以设置模型,以便 Ext Js 访问 /users/{userId}/account 而无需使用每个父 userId 手动更新帐户代理 url?