Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttle scroll events. Optimize scroll performance #666

Merged
merged 1 commit into from
Jun 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"oclazyload": "0.3.8",
"ngprogress": "~1.0.7",
"angular-debounce": "~1.0.0",
"angular-scroll": "0.7.0"
"angular-scroll": "0.7.0",
"lodash": "~3.9.3"
},
"devDependencies": {},
"resolutions": {
Expand Down
17 changes: 16 additions & 1 deletion lib/app/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,26 @@ angular.module('sgApp', [
modules: window.filesConfig
});
})
.run(function($rootScope) {
.factory('lodash', ['$window',
function($window) {
// Use both methods to access _ so it will work eventhough $window is mocked
return $window._ || window._;
}
])
.run(function($rootScope, $window, lodash) {
$rootScope.currentReference = {
section: {
}
};

// Create global throttled scorll
function broadcastScrollEvent(event) {
$rootScope.$broadcast('scroll', event);
}
// Some tests replace $window with a mock, make sure that we have real window
if (typeof $window.addEventListener === 'function') {
angular.element($window).bind('scroll', lodash.throttle(broadcastScrollEvent, 350));
}
})
.filter('addWrapper', ['Styleguide', function(Styleguide) {
return function(html) {
Expand Down
2 changes: 1 addition & 1 deletion lib/app/js/directives/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ angular.module('sgApp')
scope.section.showCSS = false;

// Listen to scroll events and update currentReference if this section is currently focused
angular.element($window).bind('scroll', function() {
scope.$on('scroll', function() {
updateCurrentReference();
});

Expand Down
1 change: 1 addition & 0 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function(config) {
'lib/app/js/components/ngprogress/build/ngProgress.js',
'lib/app/js/components/angular-debounce/dist/angular-debounce.js',
'lib/app/js/components/angular-scroll/angular-scroll.js',
'lib/app/js/components/lodash/lodash.js',
// application code
'lib/app/js/*.js',
'lib/app/js/controllers/*.js',
Expand Down