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

标签 ember.js ember-data

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

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/

相关文章:

ember.js - Ember CLI 测试助手

facebook - ember-cli 的守望者错误

javascript - Ember 。真正大型应用程序的延迟加载模板和 javascript

javascript - 如何防止 Ember Rails 模板泄漏到浏览器中?

javascript - 如何检查模型是否已更改但尚未保存到服务器? (Ember.js/Ember-数据)

ember.js - 将 JSON 对象转换为 Ember 数据模型

ember.js - 如何访问 Ember Controller 中的模型数据

ruby-on-rails - ember.js 从路由器中的 ember-data 查询中实例化第一条记录

javascript - 为什么我的自定义 header 出现 "Adapter operation failed"错误?

javascript - 未捕获的类型错误 : Object user has no method ‘_create’