This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ember_hash_exclamation.js
99 lines (75 loc) · 2.21 KB
/
ember_hash_exclamation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
@module ember
@submodule ember-old-router
*/
var get = Ember.get, set = Ember.set;
/**
Ember.HashLocation implements the location API using the browser's
hash. At present, it relies on a hashchange event existing in the
browser.
@class HashLocation
@namespace Ember
@extends Ember.Object
*/
Ember.HashExclamationLocation = Ember.Object.extend({
init: function() {
set(this, 'location', get(this, 'location') || window.location);
},
/**
@private
Returns the current `location.hash`, minus the '#!' at the front.
@method getURL
*/
getURL: function() {
return get(this, 'location').hash.substr(2);
},
/**
@private
Set the `location.hash` and remembers what was set. This prevents
`onUpdateURL` callbacks from triggering when the hash was set by
`HashLocation`.
Also, added an exclamation mark in front of the path.
@method setURL
@param path {String}
*/
setURL: function(path) {
exclam_path = '!'+path;
get(this, 'location').hash = exclam_path;
set(this, 'lastSetURL', exclam_path);
},
/**
@private
Register a callback to be invoked when the hash changes. These
callbacks will execute when the user presses the back or forward
button, but not after `setURL` is invoked.
@method onUpdateURL
@param callback {Function}
*/
onUpdateURL: function(callback) {
var self = this;
var guid = Ember.guidFor(this);
Ember.$(window).bind('hashchange.ember-location-'+guid, function() {
var path = location.hash.substr(2);
if (get(self, 'lastSetURL') === path) { return; }
set(self, 'lastSetURL', null);
callback(path);
});
},
/**
@private
Given a URL, formats it to be placed into the page as part
of an element's `href` attribute.
This is used, for example, when using the {{action}} helper
to generate a URL based on an event.
@method formatURL
@param url {String}
*/
formatURL: function(url) {
return '#!'+url;
},
willDestroy: function() {
var guid = Ember.guidFor(this);
Ember.$(window).unbind('hashchange.ember-location-'+guid);
}
});
Ember.Location.registerImplementation('hash_exclamation', Ember.HashExclamationLocation);