针对特定模型的自定义REST URL

Wil*_*Wit 5 rest ember.js ember-data

在Ember中是否有办法为特定模型配置自定义REST URL?

与此型号一样:

App.Post = DS.Model.extend({
    title: DS.attr('string'),
    comments: DS.hasMany('App.Comment')
});
App.Comment = DS.Model.extend({
    content: DS.attr('string'),
    post: DS.belongsTo('App.Post')
});
Run Code Online (Sandbox Code Playgroud)

而这个商店:

app.Store = DS.Store.extend({
    revision : 11,
    adapter : DS.RESTAdapter.extend({
        namespace : 'rest'
    })
});
Run Code Online (Sandbox Code Playgroud)

我希望通过/rest/post/{id}/comments而不是/rest/comments默认行为来检索注释.
是否可以为一个特定模型配置rest-url?

Tor*_*ups 1

这实际上只是针对整个应用程序中的一个模型,还是您的 REST 后端使用的默认“hasMany”uri?我问这个问题是因为我的 api(django Rest 框架)使用了这个确切的 uri,并且它需要对 ember-data 项目进行完整的拉取请求,因为要构建 URL,适配器需要相关的“父级”或“所有者”(rails 开发人员从来没有这样做过)需要,所以它不存在)。

我会编写您自己的适配器(只需对基本适配器进行子类化,以便您只覆盖不同的单个 hasMany)。我为适配器编写的方法如下,这是我完整的适配器供参考。

顺便说一句,这是 ember-data 版本 11 友好的(尚未升级到 12)

https://github.com/toranb/ember-data-django-rest-adapter/blob/master/tests/adapter.js

    findMany: function(store, type, ids, parent) {
        var json = {}
        , root = this.rootForType(type)
        , plural = this.pluralize(root)
        , ids = this.serializeIds(ids)
        , url = this.buildFindManyUrlWithParent(store, type, ids, parent);

        this.ajax(url, "GET", {
            success: function(pre_json) {
                json[plural] = pre_json;
                Ember.run(this, function(){
                    this.didFindMany(store, type, json);
                });
            }
        });
    },
Run Code Online (Sandbox Code Playgroud)