使用Ember Data将REST请求发送到嵌套的API端点URL

 麦芽糖的-寂寞 发布于 2023-01-29 17:01

如果你想象两个模型定义如下:

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');
                });
            });
        });
    });
});

...以及用于保存已编辑电子邮件的控制器操作,如下所示:

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]

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

1 个回答
  • 我想出的解决方案就是在REST适配器中重写createRecord,updateRecord和deleteRecord.

    我为受影响的模型添加了"父"属性.在*Record钩子中,我可以检查是否已设置并相应地编辑发送到buildURL的路径.

    我的createRecord,updateRecord和deleteRecord挂钩现在看起来类似于:

    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'
    });
    

    2023-01-29 17:04 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有