我有一个根据来自其他 API 的记录填充的表。所有这一切都很好。
现在我想在表格顶部添加一些按钮。单击这些按钮时,将再次调用 api,但这次带有一些参数。然后我希望用这些新记录更新表。我怎样才能做到这一点?
我的模板如下所示:
<div id="myEmberElement">
<script type="text/x-handlebars" id="posts">
<div id="controls">
<a href="#" id="mostrated">MostRated</a>
<a href="#" id="mostsold">MostSold</a>
</div>
<table>
<thead>
<tr>
<th>author</th>
<th>book</th>
</tr>
</thead>
<tbody>
{{#each model}}
<tr>
<td>{{author}}</td>
<td>{{book}}</td>
</tr>
{{/each}}
</tbody>
</table>
</script>
</div>
我的 App.js 是这样的:
App = Ember.Application.create({});
App.rootElement = "#myEmberElement";
App.Router.map(function() {
this.resource("posts");
});
App.Request = DS.Model.extend({
author: DS.attr("string"),
book: DS.attr("string")
});
App.PostsRoute = Ember.Route.extend({
model: function(){
return App.Request.find();
}
});
App.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo("posts");
}
});
App.Store = DS.Store.extend({
adapter: DS.RESTAdapter.extend({
url: '/myapp'
})
});
我试过的
我在我的 App.js 中添加了以下内容,但这并没有帮助:
$("#mostrated").click(function (e) {
e.preventDefault();
alert('came here');
App.Request.find({filter: "mostrated"})
});
现在mostrated
单击时,我可以看到向服务器发出了新请求,myapp/requests?filter=mostrated
并且数据也从服务器返回。
问题
问题是该表没有重新填充新数据。它仍然有旧数据。如何强制{{#each model}}
再次填充数据?此应用程序的示例(带有本地数据)也在 jsbin 上:http: //jsbin.com/OcAyoYo/2/edit