1
演示:http://jsbin.com/zexopa/1/edit?html,js,output如何在beforeModel中使用Ember查詢參數並選擇?
我在我的應用程序中使用查詢參數。 queryParameters
是'name'
和'category'
。
'name'
參數用於選擇和'category'
使用輸入,但如果我將其設置爲默認值,則選擇'name'
時出現問題。
如果我更改'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>
你能幫忙檢查會發生什麼我的應用程序?
謝謝。我的產品來自我的服務器,所以我在這裏使用'beforeModel'。而像'[{id:1,name:'name'},{id:2,name:'name2'}]'這樣的數據,如果我想使用select,我必須將'id'設置爲'String' ? – JeskTop
如果您有權訪問API,則可以執行此操作。但在某些情況下,這可能是不可能的。你可以使用計算屬性來從服務器格式化數據,並將其用作'queryParams'的輸入。我在那個JSBin中爲你寫了一個例子 - http://jsbin.com/lixili/2/edit?html,js,console,output –
這很好。謝謝。 – JeskTop