2014-09-19 3 views
1

레일 기반 웹 서비스를 사용하는 Ember 앱이 있습니다.Ember에서 enum 값로드 및 사용

레일쪽에는 열거 형이 있습니다. 배열은 단순히 배열입니다. 이제 Ember 앱에서 해당 열거 형을 검색하여 선택 값으로 렌더링하고 싶습니다. 엠버 측면에서

{"grades":["cp","ce1","ce2","cm1","cm2"]}

get '/grades.json'

, 나는 이런 GradesRoute 생성 :

웹 서비스는 JSON 응답을 반환 그런

App.GradesRoute = Ember.Route.extend({ 
    model: function() { 
     return Em.$.getJSON('api/v1/grades.json') 
    } 
})); 

을, 내가 필요가 있다고 생각 이 열거 형이 사용되는 컨트롤러의 경우 :

App.StudentsController = Ember.ArrayController.extend({ 
    needs: ['grades'], 
    grades: Ember.computed.alias('controllers.grades') 
} 
)); 

최소한 나는 students 템플릿의 성적을 반복 할 수 있다고 생각했습니다.

{{#each grade in grades}} 
    {{grade}} 
{{/each}} 

하지만 더에서 모든 출력 ... 디버깅 템플릿에서와 노력 templateContext.get를 ('등급') 얻을 것은. ('모델')을 얻는 것은 하늘의 배열 []

어떤 생각을 반환 이 데이터를 어떻게로드하고 액세스 할 수 있습니까?

답변

0

일부 경로 만 섞어서 사용하면됩니다. StudentsController에서 controllers.grades은 모델이 아닌 실제 컨트롤러를 나타냅니다. 다음 코드는 이름 짓기에 좀 더 명료하기 때문에 일을 정리해야합니다.

App.StudentsController = Ember.ArrayController.extend({ 
    needs: ['grades'], 
    gradesController: Ember.computed.alias('controllers.grades'), 
    grades: Ember.computed.alias('gradesController.model.grades') 
}); 

또한, 당신의 grades 경로가 students 경로의 직접적인 부모 인 경우 needs를 사용하는 경우에만 작동하는지 유의하십시오. 직접적인 부모가 아닌 경우 원하는 데이터를 다시 얻을 수 없습니다.

+0

감사합니다. 성적이 다른 모델들 사이에 공유 된 자원이어야하기 때문에 나는 잘못된 길을 가고 있다고 생각합니다. 성적 경로를 학생들의 학부모로 설정하는 것은 의미가 없습니다. 애플리케이션 컨트롤러를 사용하여 성적을 제어하려고 노력할 것입니다. – demental

1

그래서 StudentsRoute의 직계 부모 인 ApplicationRoute로 끝났습니다. 따라서이 경우에는 관련성이 필요합니다.

App.ApplicationRoute = Ember.Route.extend({ 
    setupController: function(controller) { 
    Em.$.getJSON('api/v1/enums.json').then(function(data){ 
     controller.set('grades', data['grades']); 
     controller.set('states', data['states']); 
    } 
    } 
}); 

이제 전체 앱에서 사용할 필요가있는 각 열의 별칭을 만들 수 있습니다.

App.StudentsController = Ember.ArrayController.extend({ 
    needs: ['application'], 
    grades: Ember.computed.alias('controllers.application.grades'), 
    states: Ember.computed.alias('controllers.application.states') 
}); 

저는 아직 갈 길이 멀다고 확신하지 못합니다. 어떤 제안이라도 환영합니다!

+0

여기 커피 스크립트로 전환하는 것이 혼란 스럽습니다. 구문과 일치하도록 질문 (또는이)을 업데이트 할 수 있습니다. –

+0

유감스럽게도, 나의 게으름 ... 원래 Emberscript를 사용하여이 프로젝트를 작업했지만 javascript로 되돌아갔습니다. 여기 같은 장소에서 공유하고 토론하는 것이 더 쉬운 이유 중 하나입니다. 편집이 완료되었습니다. – demental