小编red*_*m13的帖子

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

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

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')
});
Run Code Online (Sandbox Code Playgroud)

...和REST适配器:

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

...路由设置如下:

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');
                });
            });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

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

App.EmailEditController = Ember.ObjectController.extend({
    actions: { …
Run Code Online (Sandbox Code Playgroud)

ember.js ember-data

6
推荐指数
1
解决办法
3199
查看次数

标签 统计

ember-data ×1

ember.js ×1