2013-02-20 3 views
4

"/ services"및 "/ services/: service"라는 두 개의 경로가있는 응용 프로그램이 있다고 가정 해 봅니다.동적으로 콘센트 만들기

사용자가 : 서비스 상태를 방문하면 특정 서비스와 관련된 정보 목록을 볼 수 있습니다. 지금까지, 사소한.

동일한 사용자가/services를 방문하면 (모든 서비스에 대해)의 서비스 목록을 표시하고 싶습니다. 템플릿 복제없이이 작업을 수행하고 싶습니다.

명백한 대답은 아울렛 인 것처럼 보였습니다. 그러나 모든 콘센트에 정적으로 이름을 붙여야하는 것처럼 보였습니다. 이전에 알 수없는 번호가 있습니다. 그들은 모두 고유 한 이름을 가지고 있지만 물론 템플릿의 이름을 하드 코딩 할 수는 없습니다. 콘센트 이름을 동적으로 결정하고 싶습니다. renderTemplate을 적절히 작성하기 위해 this.render를 적절하게 호출 할 수 있습니다. 그러나이를 수행하는 방법을 모르겠습니다.

무엇을 제안하겠습니까?

업데이트 : {{template}}을 사용하여 특정 사례를 처리 할 수 ​​있지만 다른 경로와 연결되지는 않습니다. 나는 아직도 그것을하는 방법을 알고 싶다; 나중에 더 복잡한 작업을 수행 할 것입니다. 어디에서 더 잘 맞는 지, 각 컨트롤러에는 여전히 코드가 중복되어 있습니다.

업데이트 : 결국 같은, 내 자신의 'dynOutlet'도우미를 만든 :

// Define "dynamic outlets". These are just like normal outlets, 
// except they dereference the outlet name. 
Handlebars.registerHelper('dynOutlet', function(property, options) { 
    Ember.assert("Syntax: {{dynOutlet <property>}}", arguments.length === 2); 
    var context = (options.contexts && options.contexts[0]) || this, 
     normalized = Ember.Handlebars.normalizePath(
     context, property, options.data), 
     pathRoot = normalized.root, 
     path = normalized.path, 
     value = (path === 'this') ? pathRoot : Ember.Handlebars.get(
     pathRoot, path, options); 
    return Ember.Handlebars.helpers.outlet(value, options); 
}); 

내가 비록 내가 원하는 방식으로 작동 있을지 알아 내기 위해 아직했습니다.

+1

jsFiddle 좀 부탁해 주시겠습니까? – Wildhoney

+0

jsfiddle 작업을 수행하는 데 문제가 있습니다. 어쩌면 http://jsfiddle.net/qU44U/는 일하지 않더라도 아이디어를 줄 것입니다. – Baughn

답변

1

나는 또한 동적 인 출구를 정의 할 필요가있었습니다.

<div> 
    {{title}} <!-- accessing title property of context --> 
    {{dynamicOutlet outletName}} <!-- accessing the property that should be used as name of the outlet --> 
</div> 

도우미가 가능 콘센트 이름으로 모델의 속성을 사용할 수 있습니다 :

Ember.Handlebars.registerHelper('dynamicOutlet', function(property, options) { 
    var outletSource; 

    if (property && property.data && property.data.isRenderData) { 
     options = property; 
     property = 'main'; 
    } 

    outletSource = options.data.view; 
    while (!(outletSource.get('template.isTop'))){ 
     outletSource = outletSource.get('_parentView'); 
    } 

    options.data.view.set('outletSource', outletSource); 

    var firstContext = options.contexts.objectAt(0); 

    options.hash.currentViewBinding = '_view.outletSource._outlets.' + firstContext.get(property); 

    var controller = App.__container__.lookup("controller:movies"); 
    options.contexts.clear(); 
    options.contexts.addObject(controller); 
    return Ember.Handlebars.helpers.view.call(controller, Ember.Handlebars.OutletView, options); 
}); 

이 내가 내 템플릿에서 그것을 사용하는 방법은 다음과 같습니다이 내가 구현 된 도우미입니다.