samedi 26 mars 2016

Arbitrary query params on Ember.js

In Ember.js, query-params can be used with link-to to generate links that include a query string. For instance:

<p>{{link-to "Search" 'search' (query-params q='london')}}</p>

What is not immediately apparent is how to pass arbitrary parameters to query-params. This is: passing an object whose key/values will make the query string. For example the following (which throws an error):

// At the component
myParams: {
  q: 'london',
},

{{!-- At the template --}}
<p>{{link-to "Search" 'search' (query-params myParams)}}</p>

<!-- The end result -->
<p><a href="/search?q=london">Search</a></p>

Looking at Ember's source code, I can see how this can be done. I can put together an object that looks like those returned by query-params. True enough, the following object will be interpreted by link-to the way I want:

// At the component
myParams: {
  isQueryParams: true,
  values: {
    q: 'london',
  },
},

{{!-- At the template --}}
<p>{{link-to "Search" 'search' myParams}}</p>

However, that looks like an internal, private interface to me. So my question is: is there an approved way to do this? (And what is it?)




Aucun commentaire:

Enregistrer un commentaire