如果你想象两个模型定义如下:
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]
解决方法
我想出的解决方案就是在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,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,record,{ includeId: true }); return this.ajax(this.buildURL(path),"POST",{ data: data }); },updateRecord: function(store,record) { if(!record.get('parent') || null === record.get('parent')){ return this._super(store,record); var id = record.get('id'); return this.ajax(this.buildURL(path,id),"PUT",deleteRecord: function (store,record) { if (!record.get('parent')) { return this._super(store,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,"DELETE"); } });
示例中的电子邮件模型类似于:
App.Email = DS.Model.extend({ address: DS.attr('string'),user: DS.belongsTo('user'),parent: 'user' });