panthro
panthro

Reputation: 24061

Event Listener?

I have an infinite scroll class:

(function(){
"use strict";

var InfiniteScroll = function() {
    this.init();
};

var p = InfiniteScroll.prototype = mc.BaseClass.extend(gd.BaseClass);
p.BaseClass_init = p.init;

/*
 * Public properties
 */
p.loading = false;

/* 
 * Public methods
 */
p.init = function() {
    // Super
    this.BaseClass_init();

    // Init
    this.ready();

};

p.ready = function() {

    this._initInfiniteScroll();

};

p._initInfiniteScroll = function() {

    $(window).scroll(function(){  
        if($(window).scrollTop() == $(document).height() - $(window).height()){

            if(!p.loading)
            {
                p.loading = true;
                //send a message up to say loading
            }

        }  
    });   

}


mc.InfiniteScroll = InfiniteScroll;
}(window));

Now this is called from another class by:

this.infiniteScroll = new mc.InfiniteScroll();

In the other class I wish to listen for when the scroll is fired from where I have the comment: //send a message up to say loading

I was just wondering how I could go about this, I'm familiar with event listeners in AS3, but could someone point me in the right direction for js?

Upvotes: 1

Views: 121

Answers (1)

Rescribet
Rescribet

Reputation: 392

You can implement a custom event handler in your class on which you add listener functions in other classes like in this post.

Which you would use in your _initInfiniteScroll function like:

//send a message up to say loading
this.fire({ type: "ContentLoadingEvent" });

Though, as suggested in the post, creating a seperate class for your InfiniteScroll events is better.

Upvotes: 1

Related Questions