渲染模板并在Ember.js中调用模型钩子 [英] Rendering a template and invoking model hook in Ember.js
问题描述
我正在使用JSON请求来填充名为示例的模板。
I am using JSON requests to populate the model for a template called example.
App.ExampleRoute = Ember.Route.extend({
model: function() {
var url = "example/GetExamples";
return Ember.$.getJSON(url).then(function(data) {
return data;
});
}
});
示例模板完美呈现此数据。现在我的后端可能会随时改变,因为我有另一个模板称为addExample。它只是向服务器发送一个JSON请求,从另一个模板在数据库中添加一个条目。添加示例后,我尝试转换到示例模板,但我只看到旧数据。刷新页面时,添加条目的新数据将出现。
The example template perfectly renders this data. Now my backend might change from time to time as I have another template called addExample. It just sends a JSON request to the server to add an entry in the database from this other template. After adding the examples, I try to transition to example template but I see old data only. On refreshing the page, the new data with the added entry comes up.
App.AddController=Ember.Controller.extend({
postAddRequest: function() {
var request = $.post("/example/AddExample", {params});
request.then(this.success.bind(this), this.failure.bind(this));
},
success: function(data) {
this.transitionToRoute('example');
}
});
所以有什么办法可以强制Ember重新渲染示例模板并调用其模型钩子在路线上不刷新页面。我不能使用ember-data来保持持久性,因为它还处于beta阶段。
So is there any way I can force Ember to re render 'example' template and invoke its model hook in the route without refreshing the page. I can't use ember-data for persistence as it is still in beta stage.
推荐答案
转换到路由并发送要在路由上使用的数据。此外,路由应该使用url中的id / slug。如果没有看到
Transition to the route and send the data you'd like to use on the route. Additionally the route should be using an id/slug in the url. If not see below
this.transitionToRoute('example', data);
如果您已经在路由上,并且想要更新当前设置的控制器的型号,从控制器
If you are already on the route and you want to update the model of a currently set controller you can grab that controller and set the model.
App.SomeController = Em.Controller.extend({
needs: ['example']
someFunction: function(){
var model = ... get new model;
this.get('controllers.example').set('model', model);
}
});
这篇关于渲染模板并在Ember.js中调用模型钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!