Ember快照无法解析belongsTo Relationships

问题描述 投票:0回答:1

我正在使用ember 2.18,我试图访问嵌套的api。当我试图访问,我覆盖适配器中的buildUrl。我有两个模型程序和课程有一对多关系的程序有很多课程

问题即将到达我试图访问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} )
});

adapter/迷信

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为空意味着它没有正确加载

javascript ember.js ember-data adapter
1个回答
0
投票

感谢Jelhan,我设法使用belongsTo方法修复它,所以使用{id:true}调用快照,其中我得到父关系的id

snapshot.belongsTo(`${ancestorType}`,{id:true} );
© www.soinside.com 2019 - 2024. All rights reserved.