2013-08-21 2 views
2

부모님 템플릿 내부에서 속성을 변경하는 간단한 질문이 있습니다.Ember.js : 부모 템플릿 속성 업데이트

  • 부모
  • parent.index
  • parent.child1
  • parent.child2
  • :이 라우터는 다음과 같은 경로를 생성

    App.Router.map(function() { 
        this.resource('parent' , { path: "/parent" }, function() { 
        this.route('child1'); 
        this.route('child2'); 
        this.route('child3'); 
    }); 
    

    : 내가 쓴 코드는 아래에 적혀있다

  • parent.child3

다음은 내 템플릿입니다 (간체)

<script type="text/x-handlebars" data-template-name="application"> 
{{outlet}} 
</script> 

<script type="text/x-handlebars" data-template-name="parent"> 
    <h1>{{title}}</h1> 

    Common html 

    {{outlet}} 
</script> 

<script type="text/x-handlebars" data-template-name="parent/index"> 
    Parent specific content 
</script> 

<script type="text/x-handlebars" data-template-name="parent/child1"> 
    Child 1 specific content 
</script> 

<script type="text/x-handlebars" data-template-name="parent/child2"> 
    Child 2 specific content 
</script> 

<script type="text/x-handlebars" data-template-name="parent/child3"> 
    Child 3 specific content 
</script> 

그리고 여기 내 컨트롤러입니다

내가있을 때 템플릿 = "부모"내부 {{제목}} 속성을 업데이트 할 수있는 방법
App.ParentController = Ember.Controller.extend({ 
    title: 'Parent Title' 
}); 

App.ParentIndexController = Ember.Controller.extend({ 
    title: 'Parent Index Title' 
}); 

App.Child1Controller = Ember.Controller.extend({ 
    title: 'Child 1 Title' 
}); 

App.Child2Controller = Ember.Controller.extend({ 
    title: 'Child 2 Title' 
}); 

App.Child3Controller = Ember.Controller.extend({ 
    title: 'Child 3 Title' 
}); 

하위 컨트롤러 안에 있습니까? 나는이

App.Child3Controller = Ember.Controller.extend({ 
    needs: ['parent'], 
    init: function() { 
    this.get('controllers.parent').set('title', 'Child 3 Title'); 
    } 
}); 

처럼 뭔가를 시도했습니다하지만 부모 템플릿을 업데이트하지 않습니다. 그럼 어떻게해야합니까?

+0

내 대답은 당신을 위해 밖으로 일 했나요? – intuitivepixel

답변

3

좋아, 당신의 의견을 한 후 내 대답을 편집, 당신이 직면하고있는 문제는 이것이 당신이 당신의 parent.child3 경로에 setupController 훅을해야 할 일에 대한 있도록 Child3Controller에서 init, 당신이 그것을 필요로하는 시간을 호출되지된다

App.ParentChild3Route = Ember.Route.extend({ 
    setupController: function(controller, model) { 
    // the Ember.run.later is not necessary and is only to simulate a delay 
    // so you can actually see the title change, it would be actually 
    // just fine to call this.controllerFor('parent').set('title', 'Child 3 Title'); 
    // directly when the setupController hook is invoked 
    Ember.run.later(this, function() { 
     this.controllerFor('parent').set('title', 'Child 3 Title'); 
    }, 1500); 
    } 
}); 
는 나는 또한 index 경로에서 parent.child3 경로로 리디렉션하고있어

setupController 후크 실제로 화재를 만들기 위해, 그러나 이것은 단순히 다른 방법으로 parent.child3 경로로 이동하여도 일어날 수 :

App.IndexRoute = Ember.Route.extend({ 
    afterModel: function() { 
    this.transitionTo('parent.child3'); 
    } 
}); 

결론적으로 제어기의 값 변경은 제어기 경로의 setupController 후크에서 수행되어야합니다.

간단한 demo에서 시뮬레이션 해 보았습니다.보세요.

희망이 있습니다.

+0

나는 그것을 시도했다. 죄송합니다 내가 코드를 업데이 트합니다 – cocheese

+0

친절하게 이것을 확인 [데모] (http://jsbin.com/IxOsERE/1/edit) – seenivasan

+0

모든 답장을 보내 주셔서 감사합니다! 너희들은 나에게 아이디어를주고 내 질문 : 여기 http://jsbin.com/AhOT/3/ 당신은 내가 그것을 해결하는 또 다른 방법을했다 볼 수 있듯이을 확인 해결) – cocheese

0

모든 답장을 보내 주셔서 감사합니다. 너희들은 나에게 아이디어를 주었고 내 질문을 해결했다 :) 여기에서 그것을 확인하십시오 jsbin.com/AhOT/3 당신이 볼 수 있듯이 그것을 해결할 다른 방법을 취했다;)