Javascript Ember JS在加载新数据之前短暂地路由旧数据
我的问题是,当用户转到我的路由书/:id/:version时,需要花费一些时间来提取JSON,并且在很短的一秒钟内,它仍然呈现旧数据,然后用新数据替换它 这是我的路线:Javascript Ember JS在加载新数据之前短暂地路由旧数据,javascript,ember.js,Javascript,Ember.js,我的问题是,当用户转到我的路由书/:id/:version时,需要花费一些时间来提取JSON,并且在很短的一秒钟内,它仍然呈现旧数据,然后用新数据替换它 这是我的路线: App.BookRoute = Ember.Route.extend({ setupController: function (controller, model) { // This gets the entire JSON for the single book Ember.$.ge
App.BookRoute = Ember.Route.extend({
setupController: function (controller, model) {
// This gets the entire JSON for the single book
Ember.$.getJSON('/book?id=' + model.id + '&version=' + model.version,
function (data) {
// Set the json to the model
controller.set('model', data);
});
}
});
这是我的路由器:
App.Router.map(function () {
// Homepage (All the books)
this.resource('index', { path: '/' });
// Single Book view
this.resource('book', { path: '/book/:id/:version' });
});
例如,在第一次访问#/book/2/1时,它工作得很好。下次访问另一本书#/book/3/1时,它将快速显示#/book/2/1的数据(呈现的html模板),然后加载#/book/3/1的数据
用户离开后如何清除视图?或者如何使其不在路线/视图中显示以前加载的书籍
谢谢
编辑(添加了可能的相关问题):
另外,我还有另一个问题,可能与此相关,也可能与此无关,但是在将实际HTML呈现给DOM之前调用了didInsertElement事件。我认为这个方法是在HTML呈现到DOM之后调用的
这是一种观点:
App.BookView = Ember.View.extend({
didInsertElement: function () {
console.log('inside didInsertElement');
}
});
这听起来像是因为你在你的设置控制器钩子里这么做。获取这样的数据应该在模型钩子中完成。本指南告诉您如何执行此操作,请参阅“动态路线”部分: 试着这样做
App.Router.map(function () {
// Homepage (All the books)
this.resource('index', { path: '/' });
// Single Book view
this.resource('book', { path: '/book/:book_id/:version' });
});
App.BookRoute = Ember.Route.extend({
model: function(params) {
return Ember.$.getJSON('/book?id=' + params.book_id + '&version=' + params.version);
}
});
我实际上认为:id作为参数是保留的,因为建议您执行类似于
:book_id
的操作,您如何导航到路线?我假设您正在发送一个虚拟模型,其中包含irc上@alexspeller的book_id
和version
引用:“路由定义中的参数通常应命名为“/path/:foo_id”,以_id结尾很重要”,因此,如果有什么,重要的是只有在不使用id的情况下,或者如果您不想覆盖默认的序列化挂钩。