Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 多态关系ember数据url端点调用混淆_Ember.js - Fatal编程技术网

Ember.js 多态关系ember数据url端点调用混淆

Ember.js 多态关系ember数据url端点调用混淆,ember.js,Ember.js,嗨,我正在尝试建立多态关系 用户有一个配置文件,可以是管理员、联系人点 在下面提到的sessionAccount服务中 在调用user.get('profile')时,我的理解是余烬会巧妙地调用/pointOfContacts/1,但它正在调用/profiles/1 //序列化程序/application.js import { ActiveModelSerializer } from 'active-model-adapter'; export default ActiveModelSeria

嗨,我正在尝试建立多态关系 用户有一个配置文件,可以是管理员、联系人点

在下面提到的sessionAccount服务中 在调用
user.get('profile')
时,我的理解是余烬会巧妙地调用
/pointOfContacts/1
,但它正在调用
/profiles/1

//序列化程序/application.js

import { ActiveModelSerializer } from 'active-model-adapter';
export default ActiveModelSerializer.extend();
import ActiveModelAdapter from 'active-model-adapter';
export default ActiveModelAdapter.extend();
//适配器/application.js

import { ActiveModelSerializer } from 'active-model-adapter';
export default ActiveModelSerializer.extend();
import ActiveModelAdapter from 'active-model-adapter';
export default ActiveModelAdapter.extend();
//model.user.js

import DS from 'ember-data';
const {
  attr,
  belongsTo
}=DS;
export default DS.Model.extend({
  email: attr('string'),
  profile: belongsTo('profile',{polymorphic:true , async: true})
});
//model/profile.js

import DS from 'ember-data';
export default DS.Model.extend({

});
//接触点模型

import DS from 'ember-data';
import Profile from './profile';
const {
  attr,
} = DS;
export default Profile.extend({
  firstName: attr('string'),
});
因为ember cli mirage还不支持多态关系。 我的幻影配置是这样的

 this.get('/users/:id',(schema,request)=>{
    let record = schema.db.users.find(request.params.id);
    let { id, email, token , profileId, profileType } = record;
    let profileTypeModel = pluralize(profileType);
    let profileRecord = schema.db[profileTypeModel].find(profileId);
    let userRecord = {  id:id,
        email:email,
        token:token,
        profile_id: profileId,
        profile_type: profileType
    };
    let formattedResponse = { user: userRecord };
    formattedResponse[profileTypeModel] = [profileRecord];

    return formattedResponse;
  });
{
  "user": {
    "id": "1",
    "email": "some@email.com",
    "profile_id": 1, // these two lines
    "profile_type": "pointOfContact" // tell Ember Data what the polymorphic
                            // relationship is.
  },
  "pointOfContacts": [{
    "id": 1,
    "firstName": "somefirstname"
  }]
}
因此,我对user/:id的json响应如下所示

 this.get('/users/:id',(schema,request)=>{
    let record = schema.db.users.find(request.params.id);
    let { id, email, token , profileId, profileType } = record;
    let profileTypeModel = pluralize(profileType);
    let profileRecord = schema.db[profileTypeModel].find(profileId);
    let userRecord = {  id:id,
        email:email,
        token:token,
        profile_id: profileId,
        profile_type: profileType
    };
    let formattedResponse = { user: userRecord };
    formattedResponse[profileTypeModel] = [profileRecord];

    return formattedResponse;
  });
{
  "user": {
    "id": "1",
    "email": "some@email.com",
    "profile_id": 1, // these two lines
    "profile_type": "pointOfContact" // tell Ember Data what the polymorphic
                            // relationship is.
  },
  "pointOfContacts": [{
    "id": 1,
    "firstName": "somefirstname"
  }]
}
现在我想把这个角色放到一个名为sessionAccount的服务中。这样我就可以在应用程序的任何地方使用它 我使用的是ember simple auth

import Ember from 'ember';
const {
  inject:{
    service
  },
  computed
} = Ember;

export default Ember.Service.extend({
  session: service(),
  store: service(),
  account: computed('session.data.authenticated.user.id',function(){
    const userId = this.get('session.data.authenticated.user.id');
    if(!Ember.isEmpty(userId)){
      return this.get('store').findRecord('user',userId).then((user)=>{
        return user.get('profile');
      });
    }
  })
});