Ember.js弃用registerImplementation,有利于App.initializer [英] Ember.js deprecation of registerImplementation in favour of App.initializer

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

问题描述



我是使用HashLocation的扩展来为Ember.js实现一个hashbang url类型。这是代码片段:

 (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这个,'lastSetURL',!+路径);
},

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

Ember。$(window).bind('hashchange.ember-location - '+ guid,function(){
Ember.run {
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;
}

}));

})();

我通过重新打开路由器来使用它:

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

但是,在运行应用程序时,我遇到以下弃用:

  DEPRECATION:不再支持使用Ember.Location.registerImplementation。使用容器注册您的自定义位置实现。 

我找不到任何有关如何执行此操作的信息。有没有人对我将要做的任何实现片段?

解决方案

根据不赞成的消息。使用容器代替。

 (function(){

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

var hashbangLocation = 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;
}

});

App.register('location:hashbang',hashbangLocation);

})();

像往常一样重新打开

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


I am using an extension of HashLocation to implement a hashbang url type for Ember.js.

Here is the code snippet:

(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;
        }

    }));

})();

I use this by reopening the Router:

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

However, on running the application, i'm hitting the following deprecation:

DEPRECATION: Using the Ember.Location.registerImplementation is no longer supported. Register your custom location implementation with the container instead.

I can't find any information on how to do this. Does anyone have any implementation snippets on what I would have to do?

解决方案

According to deprecation message. use container instead.

(function() {

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

var hashbangLocation = 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;
        }

    });

App.register('location:hashbang', hashbangLocation);

})();

Reopen it as usual

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

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

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