2016-06-14 3 views
0

컨트롤러가 ('모델러'경로로) 전환 될 때마다 컨트롤러 동작을 트리거해야하지만 제대로 작동하지 않을 수 있습니다.Ember js - 경로가 활성화 될 때 컨트롤러 동작을 트리거하는 방법

var Router = Ember.Router.extend({ 
    location: config.locationType, 
    actions: { 
    loading: function() { 
     this.controllerFor('modeller').send('loading'); 
    }, 
    didTransition: function() { 
     this.controllerFor('modeller').send('didTransition'); 
    } 
    } 
}); 

Router.map(function() { 
    this.route('modeller', { path: 'modeller'}); 
    this.route('welcome', { path: '/'}); 
}); 
export default Router; 

그리고 내 모델러 컨트롤러 : 내 router.js 파일입니다

export default Ember.Controller.extend({ 
    needs: 'modeller', 

    loading: function() { 
    console.log("loading") 
    }, 

    didTransition: function() { 
    console.log("didTransition") 
    }, 
}); 

하지만 콘솔에 로그인 할 때 그 작업 중 하나를 얻을 수 없습니다. 모든 포인터 크게 감사.

UPDATE :

아마 내가 엠버 2.1.0를 사용하고 언급해야한다!

그래서 내가 사용한 솔루션은 setupController 였지만 app/routes/modeller.js 아래의 특정 모델러 경로 파일에도이 코드를 넣어야했습니다. 이전에는 app/router.js에서 모든 작업을 수행하려고했지만 거기에서 작동하지 않았습니다. 그래서 최종 솔루션은 다음과 같습니다

# app/routes/modeller.js 
import Ember from 'ember'; 
export default Ember.Route.extend({ 

    setupController: function(controller, model) { 
    this.controllerFor('modeller').activate(); 
    } 
}); 

와 ..

# app/controllers/modeller.js 
export default Ember.Controller.extend({ 
    needs: 'modeller', 

    activate: function() { 
    console.log("activate") 
    }, 
}); 

답변

2

didTransition 오히려 그 메서드가 호출 받고되지 않는 이유입니다 Controller보다 Route의 방법.

경로에서 컨트롤러를 실행해야하는 경우 원하는 후크는 setupController이됩니다.

구성 요소를 사용하는 것이 가장 좋은 방법은 components입니다.

1

경로에서 willTransition을 사용해보십시오.

전환이 시도되면 {{link to}}, transitionTo 또는 URL 변경을 통해 현재 활성 경로에서 willTransition 액션이 실행됩니다. 이렇게하면 최하위 루트부터 시작하여 전환이 발생할지 여부를 결정할 수있는 기회가 각 활성 경로에 제공됩니다.

https://guides.emberjs.com/v2.4.0/routing/preventing-and-retrying-transitions/

모델 후크

이 &이 Ember.Route의 후크를 비활성화 활성화는이 두 가지 중 하나는 당신이 사용할 수있는, 경로 사이의 모든 전환을 호출하고 안에 당신이 컨트롤러를 호출 할 수 있습니다 액션 방법.

deactivate: function() { 
    this._super(); 
    this.get('controller').send('actionName'); 
} 
관련 문제