gpt4 book ai didi

ember.js - RESTAdapter : how to create resources with nested path

转载 作者:行者123 更新时间:2023-12-04 12:54:20 28 4
gpt4 key购买 nike

我想知道如何创建一个在其余 api 中具有嵌套路径的资源。

例如,给定员工资源的终点,
/companyes/{company}/employees/{employee}

如何创建员工记录。
我创建了一个员工模型,但请求将发送到顶层/。
将带有员工数据的 post 请求发送到/。

我应该覆盖适配器上的 pathForType() 吗?

最佳答案

如果您在谈论 ember 路由器路径,那么我就是这样做的:

App.Router.map(function () {
this.resource('companies', {path: 'companies/:company_id'}, function () {
this.resource('employees', {path: 'employees/:employee_id'});
});
});

这会创建类似:index.html/#/companies/{company}/employees/{employee}

这将创建嵌套在该路由中的 CompaniesRoute 和 EmployeesRoute。如果您已经设置了它,则可以在 ember 检查器中很好地查看它。

Ember 将首先进入 CompaniesRoute,然后进入 EmployeesRoute。

routing documentation here

如果您正在谈论将数据发送到 REST API 服务器,那么一种解决方案是覆盖 buildURL,例如:
App.Store = DS.Store.extend({
adapter: DS.RESTAdapter.extend({
host: "http://whatever",
buildURL: function (type, id) {
var url = [];
url.push(this.urlPrefix());
url.push("companies/" + App.companyID + "/employee/" + App.employeeID );
url.push(this.pathForType(type));
if (id) {
url.push(id);
}
return url.join('/') + ".json";
}
})
});

另见 RESTAdapter doco here

注意 App.companyID 必须在调用 buildURL 之前设置。我会在 CompanyRoute 中执行此操作并将其设置为这样
model: function (params) {
App.set('companyID', params.company_id);
}

关于ember.js - RESTAdapter : how to create resources with nested path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21087119/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com