-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathangular-pjax.js
72 lines (56 loc) · 1.8 KB
/
angular-pjax.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
(function() {
var angularPJAXModule = angular.module('ngPJAX', []);
var pjaxOptions = {
container: '[data-pjax-container]'
};
angularPJAXModule.provider('pjax', function() {
this.config = function(options) {
angular.extend(pjaxOptions, options);
};
this.$get = function() {
return {
navigateTo: function(url) {
$.pjax({
url: url,
container: pjaxOptions.container
});
}
};
};
});
angularPJAXModule.directive('pjaxContainer', function() {
return {
scope:true,
controller: ['$rootScope', '$scope', '$window', '$timeout', '$compile', '$element', function($rootScope, $scope, $window, $timeout, $compile, $element) {
var contentScope = $scope,
parentScope = $scope.$parent;;
var updateCurrentPath = function() {
$rootScope.currentPath = $window.location.pathname;
};
$element.attr('data-pjax-container', true);
$(document).pjax('a:not([data-remote]):not([data-behavior]):not([data-skip-pjax])', pjaxOptions);
$(document).on('pjax:start', function() {
$timeout(function() {
$rootScope.contentLoading = true;
});
});
$(document).on('pjax:beforeReplace', function() {
$timeout(function() {
if (contentScope) {
contentScope.$destroy();
}
});
});
$(document).on('pjax:end', function() {
$timeout(function() {
updateCurrentPath();
contentScope = parentScope.$new(false, parentScope);
$element.html($compile($element.html())(contentScope));
$rootScope.contentLoading = false;
});
});
updateCurrentPath();
}]
};
});
}).call(this);