Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js deprecation of registerImplementation in favour of App.initializer

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?

like image 452
Stephen Wright Avatar asked Mar 31 '14 18:03

Stephen Wright


1 Answers

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'
});
like image 110
Hung Weichien Avatar answered Oct 14 '22 07:10

Hung Weichien