klaaspieter
klaaspieter

Reputation: 2665

Get context of current route

I've implemented a search field in my ember app using ember router. The relevant portion of my router looks like this:

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)
  }
})

I want to bind the value of my search field to the current route's context. I found a way to do this in my controller using the stateMetaFor method which is implemented in my controller like this:

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()

I'm not content with this method because 1) it uses the undocumented findStateByPath method and 2) because it requires knowledge of internals of the stateMeta object. Is there a better way to do this? If there isn't, should there be?

Upvotes: 1

Views: 2742

Answers (1)

Tom Dale
Tom Dale

Reputation: 720

The correct way to display information in your views is to make it available on a controller, and have your views bind to that.

In this instance, the idiomatic thing to do would be to set a query property on the helpController:

router.set('helpController.query', query);

Upvotes: 4

Related Questions