使用 Ember.js 的 Hashbang URL [英] Hashbang URLs using Ember.js

查看:11
本文介绍了使用 Ember.js 的 Hashbang URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置我的 Router 以使用hashbang"网址 (#!).

I am trying to set up my Router to use "hashbang" URLs (#!).

我试过了,但显然它不起作用:

I tried this, but obviously it doesn't work:

App.Router.map(function() {
    this.route("index", { path: "!/" });
    this.route("otherState", { path: "!/otherState" });
});

这可以在 Ember 中实现吗?

Is this possible to do in Ember?

推荐答案

Teddy Zeenny 的回答大部分是正确的,registerImplementation 似乎是实现这一点的一种干净的方式.我试图编辑他的答案以使其完全回答问题,但我的编辑被拒绝了.

Teddy Zeenny's answer is mostly correct, and registerImplementation seems to be a clean way to implement this. I tried to just edit his answer to make it fully answer the question, but my edit got rejected.

无论如何这里是让 Ember 使用 hashbang URL 的完整代码:

Anyway here is the full code to make Ember use hashbang URLs:

(function() {

var get = Ember.get, set = Ember.set;

Ember.Location.registerImplementation('hashbang', Ember.HashLocation.extend({ 

    getURL: function() {
        return get(this, 'location').hash.substr(2);
    },

    setURL: function(path) {
        get(this, 'location').hash = "!"+path;
        set(this, 'lastSetURL', "!"+path);
    },

    onUpdateURL: function(callback) {
        var self = this;
        var guid = Ember.guidFor(this);

        Ember.$(window).bind('hashchange.ember-location-'+guid, function() {
                Ember.run(function() {
                    var path = location.hash.substr(2);
                    if (get(self, 'lastSetURL') === path) { return; }

                    set(self, 'lastSetURL', null);

                    callback(location.hash.substr(2));
                });
        });
    },

    formatURL: function(url) {
        return '#!'+url;
    }

}));

})();

创建应用后,您需要更改路由器以使用hashbang"位置实现:

Then once you create your app you need to change the router to utilize the "hashbang" location implementation:

App.Router.reopen({
    location: 'hashbang'
})

这篇关于使用 Ember.js 的 Hashbang URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆