Ember.js 获取当前路由的上下文
我已经使用ember路由器在我的ember应用程序中实现了一个搜索字段。我的路由器的相关部分如下所示:Ember.js 获取当前路由的上下文,ember.js,ember-old-router,Ember.js,Ember Old Router,我已经使用ember路由器在我的ember应用程序中实现了一个搜索字段。我的路由器的相关部分如下所示: question: Em.Route.extend({ route: '/?query=:query' connectOutlets: function(router, context) { query = context['query'], articles = App.get('defaultStore').findQuery(App.Article, {"quer
question: Em.Route.extend({
route: '/?query=:query'
connectOutlets: function(router, context) {
query = context['query'],
articles = App.get('defaultStore').findQuery(App.Article, {"query": query})
router.get('helpController').connectOutlet('articlesSearch', 'articlesSearch', articles)
}
})
query: function() {
router = App.get('router')
state = router.findStateByPath(router.get('currentState'), 'help.question')
if (router.get('currentState') == state) {
stateMeta = router.stateMetaFor(state)
return stateMeta['context']['query]
}
return ''
}.property('App.router.currentState').cacheable()
我想将搜索字段的值绑定到当前路由的上下文。我在控制器中找到了一种方法,使用stateMetaFor
方法,该方法在控制器中实现如下:
question: Em.Route.extend({
route: '/?query=:query'
connectOutlets: function(router, context) {
query = context['query'],
articles = App.get('defaultStore').findQuery(App.Article, {"query": query})
router.get('helpController').connectOutlet('articlesSearch', 'articlesSearch', articles)
}
})
query: function() {
router = App.get('router')
state = router.findStateByPath(router.get('currentState'), 'help.question')
if (router.get('currentState') == state) {
stateMeta = router.stateMetaFor(state)
return stateMeta['context']['query]
}
return ''
}.property('App.router.currentState').cacheable()
我不满足于此方法,因为1)它使用未记录的
findStateByPath
方法,2)它需要了解stateMeta对象的内部结构。有更好的方法吗?如果没有,是否应该有?在视图中显示信息的正确方法是使其在控制器上可用,并将视图绑定到该控制器
在本例中,惯用的做法是在helpController
上设置query
属性:
router.set('helpController.query', query);
在视图中显示信息的正确方法是使其在控制器上可用,并将视图绑定到该控制器 在本例中,惯用的做法是在
helpController
上设置query
属性:
router.set('helpController.query', query);