gpt4 book ai didi

ember.js - 使用 Ember Data 将 REST 请求发送到嵌套 API 端点 URL

转载 作者:行者123 更新时间:2023-12-02 21:32:07 24 4
gpt4 key购买 nike

如果您想象这样定义的两个模型:

App.User = DS.Model.extend({
emails: DS.hasMany('email', {embedded: 'always'}),
});

App.Email = DS.Model.extend({
address: DS.attr('string'),
alias: DS.attr('string'),
user: DS.belongsTo('user')
});

...和 ​​REST 适配器:

App.UserAdapter = DS.RESTAdapter.extend({
url: 'http://whatever.com',
namespace: 'api/v1'
});

...路由设置如下:

App.Router.map(function () {
this.route('index', { path: '/' });
this.resource('users', function () {
this.route('index');
this.route('add');
this.resource('user', { path: ':user_id' }, function () {
this.route('delete');
this.route('edit');
this.resource('emails', function () {
this.route('index');
this.route('add');
this.resource('email', { path: ':email_id' }, function () {
this.route('delete');
this.route('edit');
});
});
});
});
});

...以及用于保存编辑后的电子邮件的 Controller 操作,如下所示:

App.EmailEditController = Ember.ObjectController.extend({
actions: {
save: function () {
var self = this;
var email = this.get('model');
email.save().then(function(){
self.transitionToRoute('email', email);
});
}
}
});

问题是这样的...

PUT 请求正在发送至:http://whatever.com/api/v1/emails/[email_id]

但是正确的 API 端点是:http://whatever.com/api/v1/users/[user_id]/emails/[email_id]

解决此问题的正确方法是什么?

最佳答案

我想出的解决方案就是在 REST 适配器中重写 createRecord、updateRecord 和 deleteRecord。

我向受影响的模型添加了“父”属性。在 *Record Hook 中,我可以检查是否已设置并相应地编辑发送到 buildURL 的路径。

我的 createRecord、updateRecord 和 deleteRecord Hook 现在看起来与此类似:

App.UserAdapter = DS.RESTAdapter.extend({

createRecord: function (store, type, record) {

if (!record.get('parent') || null === record.get('parent')) {
return this._super(store, type, record);
}

var data = {};
var serializer = store.serializerFor(type.typeKey);

var parent_type = record.get('parent');
var parent_id = record.get(parent_type).get('id');
var child_type = Ember.String.camelize(
Ember.String.pluralize(
type.typeKey.split(
record.get('parent')
).pop()
)
);

var path = Ember.String.pluralize(parent_type) + '/' + parent_id + '/' + child_type;

serializer.serializeIntoHash(data, type, record, { includeId: true });

return this.ajax(this.buildURL(path), "POST", { data: data });
},

updateRecord: function(store, type, record) {

if(!record.get('parent') || null === record.get('parent')){
return this._super(store, type, record);
}

var data = {};
var serializer = store.serializerFor(type.typeKey);

var parent_type = record.get('parent');
var parent_id = record.get(parent_type).get('id');
var child_type = Ember.String.camelize(
Ember.String.pluralize(
type.typeKey.split(
record.get('parent')
).pop()
)
);

var path = Ember.String.pluralize(parent_type) + '/' + parent_id + '/' + child_type;

serializer.serializeIntoHash(data, type, record);
var id = record.get('id');

return this.ajax(this.buildURL(path, id), "PUT", { data: data });
},

deleteRecord: function (store, type, record) {

if (!record.get('parent')) {
return this._super(store, type, record);
}

var parent_type = record.get('parent');
var parent_id = record.get('parent_id');
var child_type = Ember.String.camelize(
Ember.String.pluralize(
type.typeKey.split(
record.get('parent')
).pop()
)
);

var path = Ember.String.pluralize(parent_type) + '/' + parent_id + '/' + child_type;
var id = record.get('id');

return this.ajax(this.buildURL(path, id), "DELETE");
}

});

示例中的电子邮件模型类似于:

App.Email = DS.Model.extend({
address: DS.attr('string'),
alias: DS.attr('string'),
user: DS.belongsTo('user'),
parent: 'user'
});

关于ember.js - 使用 Ember Data 将 REST 请求发送到嵌套 API 端点 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21530833/

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