本文介绍了emberjs只有哈希的动态路由(没有斜线)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使一切都在标题中,要清楚,我希望我的emberjs动态路线看起来像:

Even if everything is in the title, to be clear, I would like my emberjs dynamic routes to look like:

仅使用哈希值,而不是默认值为/的

with hash only, instead of the one with '/' by default:

不知道是否可能(我尝试了几个黑客没有成功),但如果是,让我知道:)

Not sure if it's possible (I tried several hacks without success) but if yes let me know :)

谢谢,
汤姆

推荐答案

最高版本1.0.0-pre.4这似乎是默认的行为。在最终版本1.0.0中,它只对没有路径的路由的行为如此。我已经遵循并且似乎工作正常的方法是提供位置API的实现(),它基于


Up to version 1.0.0-pre.4 this seemed to be the default behaviour. In final version 1.0.0 it only behaves like this for routes without paths. An approach i have followed and seems to work fine is by providing an implementation for the location API (http://emberjs.com/guides/routing/specifying-the-location-api/) and it is based on the default implementation discussed in,https://github.com/emberjs/ember.js/issues/2053answers andHashbang URLs using Ember.js

(function() {

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

    Ember.Location.registerImplementation('no-slashes', Ember.HashLocation.extend({   
        getURL: function() {
            var path = get(this, 'location').hash;
            if(path.indexOf("/")!=1){
                return "/"+path.substr(1);
            }else{
                return path.substr(1);  
            }
        },

        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(1);
                    if(path.indexOf("/")!=0){
                        path = "/"+path;
                    }
                    if (get(self, 'lastSetURL') === path) { return; }
                    set(self, 'lastSetURL', null);
                    callback(path);
                });
            });
        }

    }));

})();
App.Router.reopen({
    location: 'no-slashes'
});

这篇关于emberjs只有哈希的动态路由(没有斜线)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 16:59