2016-09-10 5 views
0

(ember-cliember-data = 2.7)을 배우고 있으며 모델을 사용하는 데 문제가 있습니다. 엠버에 따라,모델의 정의되지 않은 필드

{ 
    "data": [ 
    { 
     "attributes": { 
     "URI": "", 
     "cambios": "Commit inicial", 
     "cambiosHtml": "<p>Commit inicial</p>", 
     "contenido": "No por mucho _madrugar_, amanece más __temprano__.", 
     "contenidoHtml": "<p>No por mucho <em>madrugar</em>, amanece más <strong>temprano</strong>.</p>", 
     "firmas": { 
      ... 
      "reviso": "Zamora" 
     }, 
     "notify": false, 
     "pub_date": "2011-09-30", 
     "revision": 0 
     }, 
     "id": "7a09d345-27cc-45ef-bf58-488354c25239", 
     ... 
    ], 
    "links": { 
    ... 
    }, 
    "meta": { 
    ... 
    } 
    } 
} 

자 :이 API의 응답이

// serializers/application.js 
import DS from 'ember-data'; 

export default DS.JSONAPISerializer.extend({ 
    primaryKey: 'id' 
}); 

: 일부 모델은 다음과 같이

import DS from 'ember-data'; 

export default DS.Model.extend({ 
    revision: DS.attr('number'), 
    pub_date: DS.attr('date'), 
    contenido: DS.attr('string'), 
    contenidoHtml: DS.attr(), 
    cambios: DS.attr('string'), 
    cambiosHtml: DS.attr(), 
    notify: DS.attr('boolean'), 
    URI: DS.attr('string'), 
    firmas: DS.attr() 
}); 

내가 간단한 시리얼이 '정의되지 않은'으로 나타납니다 검사관, 필드 contenidoHtml, cambiosHtmlpub_date이며 정의되지 않음은이거나 비어 있습니다.

누락 된 것이 있습니까? 이 협약이 camelize하는 엠버 데이터에서

답변

1

는 모델

pub_date에 이름 속성 -이 그래서 pubDate의

로 변경해야 follwed되지

JSONAPISerializer는 속성이 dasherized 될 것으로 예상 서버에서 반환 한 페이로드 문서의 내용 :

cambiosHtml-cambios-html
contenidoHtml로 변경 - contenido-html
pub_date로 변경 - 당신이 API를 제어 할 수 있습니다 해달라고하면 pub-date

로 변경, 당신은 아래의 엠버를 실행하여 모델 특정 시리얼을 만들 수 있습니다 -cli 명령,

ember generate serializers 'model-name' 

응용 프로그램/시리얼/모델 name.js

import DS from 'ember-data'; 

export default DS.JSONAPISerializer.extend({ 
    attrs: { 
    cambiosHtml: 'cambiosHtml', 
    contenidoHtml: 'contenidoHtml', 
    pubDate: 'pub_date' 
    } 
}); 

https://guides.emberjs.com/v2.8.0/models/customizing-serializers/#toc_attribute-names

관련 문제