Ember.js

时间:2016-03-26 23:48:51

标签: ember.js

在Ember.js中,query-params可与link-to一起使用,以生成包含查询字符串的链接。例如:

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

如何将任意参数传递给query-params并不是很明显。这是:传递一个对象,其键/值将成为查询字符串。例如以下(抛出错误):

// 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>

看看Ember的源代码,我可以看到如何做到这一点。我可以将一个看起来像query-params返回的对象组合在一起。确实如此,link-to将按照我想要的方式解释以下对象:

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

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

然而,这看起来像是一个内部的私人界面。所以我的问题是:是否有批准的方法来做到这一点? (那是什么?)

1 个答案:

答案 0 :(得分:2)

目前唯一的解决方案是创建自己的帮手:

// app/helpers/hash-query-params.js
import Ember from 'ember';

export function hashQueryParams([hashParams]) {
  return Ember.Object.create({
    isQueryParams: true,
    values: hashParams
  });
}

export default Ember.Helper.helper(hashQueryParams);

然后像

一样使用它
<p>{{link-to "Search" 'search' (hash-query-params myParams)}}</p>

这可以得到Ember的支持。打开RFC或使用此帮助程序创建插件! :)