기존 Discourse 라우트를 테마 컴포넌트에서 리디렉션하려면 api.modifyClass를 사용하여 라우트 클래스를 수정할 수 있습니다.
더 자세한 내용은 Ember의 라우팅 리디렉션 문서에서 확인할 수 있습니다. 아래는 예시입니다:
<script type="text/discourse-plugin" version="0.8">
// 태그 라우트 수정.
api.modifyClass('route:tag-show', {
afterModel: function(tag, transition) {
this._super(tag, transition);
// 일요일인 경우 id가 20인 토픽으로 리디렉션합니다.
if (today.getDay() == 6) {
this.replaceWith("/t/20");
}
}
}
</script>
리디렉션할 위치나 리디렉션 여부를 결정하기 위해 데이터를 로드해야 하는 경우, Ember의 비동기 라우팅 문서에서 논의된 바와 같이 라우트 훅이 프로미스(Promise)를 반환하도록 해야 합니다. 아래는 예시입니다:
<script type="text/discourse-plugin" version="0.8">
const ajax = require('discourse/lib/ajax').ajax;
api.modifyClass('route:tag-show', {
afterModel: function(tag, transition) {
// 이 부분이 먼저 와야 하는 것 같습니다.
this._super(tag, transition);
// 라우트 태그와 'canonical' 태그가 모두 지정된 첫 번째 열린 토픽으로 리디렉션합니다.
var path = `/tags/intersection/canonical/${tag.id}.json`;
const context = this;
return ajax(path).then(function(result){
var topics = result.topic_list.topics.filter(function(topic) {
return !topic.closed ;
});
if (topics && topics.length > 0) {
var topic = topics[0];
// 목적지가 시작점과 동일한 경우를 고려해야 합니다.
// 트랜지션을 취소할 수도 있지만, 여기서는 리디렉션 없이 진행되도록 허용합니다.
if (!transition.from || transition.from.parent.name != 'topic' || transition.from.parent.params.id != topic.id) {
context.replaceWith("/t/" + topic.id);
}
}
})
// (리디렉션되지 않은) 라우트가 폴백으로 진행될 수 있도록 모든 문제를 잡아냅니다.
.catch(function() {
console.log('Error loading topics by ajax in tag show route afterModel hook.');
});
},
});
</script>