如何在 beforeModel 和 select 中使用 Ember 查询参数?
How to use Ember query parameters with beforeModel and select?
演示:http://jsbin.com/zexopa/1/edit?html,js,output
我在我的应用程序中使用查询参数。而 queryParameters
是 'name'
和 'category'
.
select中使用了'name'
参数,'category'
中使用了输入,但是select中有问题'name'
if我把它默认设置为空。
如果我更改 'name'
,'name'
在 url 中始终未定义。
路线:
App.IndexRoute = Ember.Route.extend({
beforeModel: function() {
this.controllerFor('index').set('products', [1,2,3]);
},
model: function() {
return [{'is_active':false, 'name':'One'}, {'is_active':false, 'name':'Two'}, {'is_active':false, 'name':'Three'}, {'is_active':false, 'name':'Four'},{'is_active':false, 'name':'Five'}];
},
actions: {
queryParamsDidChange: function() {
this.refresh();
}
}
});
控制器:
App.IndexController = Ember.Controller.extend({
queryParams: ['name', 'category'],
name: null,
category: null
});
模板:
<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
{{view "select" content=products value=name prompt="all"}}
{{input type="text" value=category class="form-control"}}
<ul>
{{#each model as |item|}}
<li>{{item.name}}</li>
{{/each}}
</ul>
</script>
你能帮忙检查一下我的应用程序发生了什么情况吗?
查询参数必须是字符串才能正确绑定。您的输入有效,因为值为 String
对象。在 name
数组中,您提供了 Integer
。不幸的是,我没有在文档中找到任何关于它的提及,但你可以在这里看到一个有效的演示:http://jsbin.com/lixili/1/edit?html,js,output
如果我可以给你一些关于你的代码的提示:
beforeModel
不是设置控制器属性的地方,在 JSBin 提供的 setupController
方法中进行
- 您没有在路由中定义查询参数,但您可以删除
queryParamsDidChange
希望我有所帮助!
演示:http://jsbin.com/zexopa/1/edit?html,js,output
我在我的应用程序中使用查询参数。而 queryParameters
是 'name'
和 'category'
.
select中使用了'name'
参数,'category'
中使用了输入,但是select中有问题'name'
if我把它默认设置为空。
如果我更改 'name'
,'name'
在 url 中始终未定义。
路线:
App.IndexRoute = Ember.Route.extend({
beforeModel: function() {
this.controllerFor('index').set('products', [1,2,3]);
},
model: function() {
return [{'is_active':false, 'name':'One'}, {'is_active':false, 'name':'Two'}, {'is_active':false, 'name':'Three'}, {'is_active':false, 'name':'Four'},{'is_active':false, 'name':'Five'}];
},
actions: {
queryParamsDidChange: function() {
this.refresh();
}
}
});
控制器:
App.IndexController = Ember.Controller.extend({
queryParams: ['name', 'category'],
name: null,
category: null
});
模板:
<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
{{view "select" content=products value=name prompt="all"}}
{{input type="text" value=category class="form-control"}}
<ul>
{{#each model as |item|}}
<li>{{item.name}}</li>
{{/each}}
</ul>
</script>
你能帮忙检查一下我的应用程序发生了什么情况吗?
查询参数必须是字符串才能正确绑定。您的输入有效,因为值为 String
对象。在 name
数组中,您提供了 Integer
。不幸的是,我没有在文档中找到任何关于它的提及,但你可以在这里看到一个有效的演示:http://jsbin.com/lixili/1/edit?html,js,output
如果我可以给你一些关于你的代码的提示:
beforeModel
不是设置控制器属性的地方,在 JSBin 提供的setupController
方法中进行- 您没有在路由中定义查询参数,但您可以删除
queryParamsDidChange
希望我有所帮助!