灰烬快照无法解析belongsToRelationships

时间:2018-08-30 09:26:47

标签: javascript ember.js ember-data adapter

我正在使用ember 2.18,而我正在尝试访问嵌套的api。 当我试图访问时,我在适配器中覆盖了buildUrl。 我有两个模型程序和课程,它们之间存在一对多的关系 计划中有很多课程

问题im到达这里是im正在尝试访问belongsTo模型的ID,这是课程中的程序ID,但它不存在。

  

程序模型

export default DS.Model.extend( {

  name:            DS.attr( 'string' ),
  code:            DS.attr( 'string' ),
  welcome:         DS.attr( 'string' ),
  startDate:       DS.attr( 'date' ),
  endDate:         DS.attr( 'date' ),
  published:       DS.attr( 'boolean' ),

  core_group:      DS.attr(),

  courses: DS.hasMany( 'course',{async: true}) });
  

课程模型

export default DS.Model.extend( {

  uid: DS.attr( 'string' ),
  name: DS.attr( 'string' ),
  abbreviation: DS.attr( 'string' ),
  startDate: DS.attr( 'date' ),
  endDate: DS.attr( 'date' ),
  country: DS.attr( 'string' ),
  timezone: DS.attr( 'string' ),
  published: DS.attr( 'boolean' ),

  programme: DS.belongsTo( 'programme', { async: true} )
});
  

适配器/混合蛋白

import Ember from 'ember';

export default Ember.Mixin.create( {
  findRecord: function( store, type, id, snapshot ) {

    return this.ajax( this.buildURL( type.modelName, null, snapshot, 'findRecord' ), 'GET' );
  },

  buildURL: function( modelName, id, snapshot, requestType, query ) {

    var local_snapshot = snapshot;
    var url          = this._super( modelName, id, snapshot, requestType, query ),
        ancestorType = this.get( 'ancestorType' ),
        namespace    = this.get( 'namespace' ),
        ancestor,
        ancestorComponent,
        position;

    console.log(local_snapshot)
    // make a big assumption here, that requests will only be for a given parent,
    // not disparate parents
    if ( local_snapshot instanceof Array ) ancestor = local_snapshot[ 0 ].get( `${ancestorType}.id` );
    else ancestor = local_snapshot.get( `${ancestorType}.id` );

    position          = url.indexOf( namespace + '/' ) + namespace.length;
    ancestorComponent = '/' + Ember.String.pluralize( ancestorType );

    if ( ancestor ) ancestorComponent = ancestorComponent + `/${ancestor}`;

    url = [ url.slice( 0, position ), ancestorComponent, url.slice( position ) ].join( '' );

    return url;
  }


});
  

适配器/课程

import ApplicationAdapter from '../adapters/application';
import NestedMixin from '../adapters/mixins/nested';


export default ApplicationAdapter.extend(NestedMixin, {
  ancestorType: 'programme'
} );

错误提示是

  

local_snapshot.get不是函数

当我查看模型belongsToRelationships为空时,表示它未正确加载

1 个答案:

答案 0 :(得分:0)

感谢Jelhan,我设法使用belongsTo方法对其进行了修复,因此使用{id:true}调用了快照,在那里我获得了父关系的ID

snapshot.belongsTo(`${ancestorType}`,{id:true} );