서버 측 (ATM은 여전히 FixturesAdapter를 사용하고 있습니다)에서 데이터를 가져와 사용자가 끌어온 형식 중 하나를 선택하여 선택하도록 두 개의 연결된 선택 입력을 만들고 싶습니다. 모든 형식에는 속한 범주가 있으므로 첫 번째 입력을 통해 사용자가 범주를 선택한 다음 두 번째 선택 입력이 업데이트되어 선택한 범주의 형식 만 포함됩니다.두 개의 연결된 선택 필드를 만드는 것은 매우 못생긴 것입니다. 내가 뭘 잘못하고있는 걸까요?
나는 그럭저럭 효과를 낼 수 있었지만, 코드는 정말 지저분 해 보였으며, 그것을 개선 할 방법이 있습니까?
도움을 주시면 감사하겠습니다. 감사합니다.
형식 모델
App.Format = DS.Model.extend({
title: DS.attr('string'),
category: DS.attr('string')
});
ApplicationRoute
App.ApplicationRoute = Ember.Route.extend({
setupController: function(controller, model) {
this.controllerFor('formats').set('model', this.store.find('format'));
}
});
formats.hbs
<label class="control-label">Category:</label>
<div class="controls">
{{view "select" content=categories value=selectedCategory}}
</div>
<label class="control-label">Format:</label>
<div class="controls">
{{view "select" content=formats value=selectedFormat}}
</div>
FormatsController
App.FormatsController = Ember.ArrayController.extend({
needs: 'index',
categories: function() {
return this.mapBy('category').uniq();
}.property('@each.title'),
observeCategories: function() {
this.set('selectedCategory', this.get('categories.firstObject'));
}.observes('categories'),
observeSelectedCategory: function() {
this.set('formats', this.filterBy('category', this.get('selectedCategory')).mapBy('title'));
}.observes('selectedCategory'),
observeFormats: function() {
this.set('selectedFormat', this.get('formats.firstObject'));
}.observes('formats'),
observeSelectedFormat: function() {
var format = this.findBy('title', this.get('selectedFormat'));
this.get('controllers.index').set('format', format);
}.observes('selectedFormat')
});
다른 모델의 형식 속성을 설정하는 데 컨트롤을 사용할 때만 의미가 있기 때문에 형식에 별도의 경로가 필요하지 않습니다. 그래서 나는 다른 컨트롤러에 대한 참조를 얻을 필요가있을 것이다. 그러나 내가 이해할 수있는 한, "필요하다"시스템을 사용해야 할 것이다. 피드백에 정말 감사드립니다. 내 주된 고통은'FormatsController'가 급격하게 줄어들 수 있는지를 보는 것이 었습니다. 왜냐하면 제가 완전히 잘못된 것을하고 있다는 느낌을 받기 시작했기 때문입니다. 다행스럽게도 계산 된 속성이있는 트릭을 보여 줘서 고맙습니다! – joncys