Ember.js Ember 1.0最终版Internet Explorer 8和9(历史API-pushState和replaceState)
我的印象是,余烬在一些旧版本的IE下进行了很好的测试。然而,当我最终启动了几乎完整的应用程序(表单向导)时。我注意到IE正在抱怨replaceState和pushState,这两种方法根据 有什么解决办法吗Ember.js Ember 1.0最终版Internet Explorer 8和9(历史API-pushState和replaceState),ember.js,Ember.js,我的印象是,余烬在一些旧版本的IE下进行了很好的测试。然而,当我最终启动了几乎完整的应用程序(表单向导)时。我注意到IE正在抱怨replaceState和pushState,这两种方法根据 有什么解决办法吗 SCRIPT438:对象不支持属性或方法“replaceState” get(这是“历史”).replaceState(state,null,path)更新:从Ember 1.5.0+开始,我可以确认他们添加了“自动”,这将消除对以下示例的需要 App.Router.reopen({ l
SCRIPT438:对象不支持属性或方法“replaceState”
get(这是“历史”).replaceState(state,null,path)代码>更新:从Ember 1.5.0+开始,我可以确认他们添加了“自动”,这将消除对以下示例的需要
App.Router.reopen({
location: 'auto'
});
原始答复:
显然,您需要检测历史API的功能:
if (window.history && window.history.pushState) {
App.Router.reopen({
location: 'history'
});
}
周围没有工作。如果您需要使用真实的URL(/users)而不是散列URL(/#/users),那么您必须从支持的浏览器列表中排除IE 8和9,或者您需要以“渐进增强”的方式使用Ember,仍然为访问的每个真实URL提供来自服务器的有效内容,并使用功能检测有选择地启用您的Ember应用。要正确支持pushState和非pushState浏览器,您需要在两种不同的状态机制之间安装一个转换器
例如,假设您的rootURL为“/admin/”,您从以下URL开始:
/管理员/用户/123
对于IE8/9,您需要在Ember的路由机制接管之前将用户重定向到“/admin/#/users/123”。同样,如果您从该URL开始:
/管理员/#/users/123
…然后,对于支持pushState的浏览器,您需要在Ember的路由机制接管之前将状态替换为“/admin/users/123”
这是主干网路由器的默认行为,它工作得相当好。
为了在Ember中实现这一结果,您可以执行类似的操作,其灵感来自主干网的源代码:
App.Router.reopen({
rootURL: '/admin/',
init: function () {
this.translateRoute();
this._super();
},
translateRoute: function () {
var hasPushState = window.history && window.history.pushState;
var atRoot = window.location.pathname === this.rootURL;
var fragment = decodeURIComponent(window.location.pathname);
if (!fragment.indexOf(this.rootURL))
fragment = fragment.substr(this.rootURL.length);
if (hasPushState)
this.location = 'history';
// If we started with a route from a pushState-enabled browser,
// but we're currently in a browser that doesn't support it...
if (!hasPushState && !atRoot) {
window.location.replace(this.rootURL + '#/' + fragment);
return;
}
// If we started with a hash-based route,
// but we're currently in a browser that supports pushState...
if (hasPushState && atRoot && window.location.hash) {
fragment = window.location.hash.replace(/^(#\/|[#\/])/, '');
window.history.replaceState({}, document.title, window.location.protocol + '//' + window.location.host + this.rootURL + fragment);
}
}
});