6

I've been working on a scrollspy module for Angularjs. I've run into the problem where if the page is dealing with dynamic content, the scrollspy data (element positions) quickly becomes outdated. What is the angularjs way of dealing with issues such as this?

Should any directive that performs DOM manipulation $broadcast an event that the scrollspy module looks out for - allowing it to refactor its position data?

Should the scrollspy module check every x seconds for a change in scrollHeight with $timeout?

Or even better, is there a way to bind and watch for DOM attribute value changes (attributes such as offsetTop, offsetHeight, scrollHeight, not data attributes)?

Update: Added code base to GitHub

2
  • Constraining other directives to emit events would likely become unwieldy quickly and certainly make your module unfriendly to work with. There isn't an event for changes to the dim of elements unfortunately, so I am afraid a polling $timeout impl is likely your best candidate. At least it is easily testable with $timeout.flush() :) Commented Oct 14, 2013 at 6:56
  • Being the devils advocate here, but what if element heights changed, but the overall scrollHeight did not... Suddenly you need to be checking everything Commented Oct 15, 2013 at 22:46

3 Answers 3

6

Mutation Observers seem to be the facility needed, unfortunately they are only supported by the latest browsers.

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
if( MutationObserver ) {

    for ( var i = 0; i < spyService.spies.length; i++ ) {
        var spy = spyService.spies[i].scope.spy;
        var target = document.getElementById(spy);

        var config = {
            attributes: true,
            childList: true,
            characterData: true,
            subtree: true
        };
        var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                console.warn('mutation observation');
            });
        }).observe(target, config);
    }
}
else {
    console.warn('no mutation observers here');
    $interval(function() {
        angular.element( document ).ready( function() {
            console.log('refreshing');
        });
    }, 2000);

}

Currently searching for a polyfill that actually works.

EDIT: Added polling as a fallback if Mutation Observers aren't supported.

Sign up to request clarification or add additional context in comments.

Comments

1

Something like this should work... doesn't seem to update with CSS transitions though.

scope.$watch(function(){
    return elem[0].offsetLeft;
}, function(x){
    console.log('ITEM AT '+x);
});

Comments

0

Without the code I'm stabbing in the dark a bit, but I would suggest your scrollspy module check every time $rootScope receives a $digest call. So use $rootScope.$watch.

$rootScope.$watch(function() {
  // Update scrollspy data here
}

3 Comments

Sorry, taken me some time to get back to this. Added link to the code on GitHub. Will this be called late enough though, once the DOM has been updated and rendered?
Yes it shouldn't get called until after AngularJS has finished loading directives and controllers and binding them.
Ran into $digest issues, probably just the current code structure however. Unfortunately this really only takes care of half the problem (assuming it works). A div that changed it's height on click for instance wouldn't be caught, unless it also called scope.$apply(); which is basically the same requirement as calling a re-calc method...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.