I make a class for pagination, I use a wrapper for jquery inside it - the class Request . The getRecords method is a callback that hangs on $(window).scroll() , inside it calls the Request class to make a request, inside of which onRequest is a callback that calls $.ajax() . Inside onRequest , this already does not contain a Pagination but contains an object containing the request data.
export class Pagination{ constructor (params? :Object){ //.. more actions this.getRecords(); fn.setOnWindowListener('scroll','pagination',null,(e) => {this.setScrollEvent(e)}); } private setScrollEvent = (e) :void =>{ let pos = $(e.currentTarget).scrollTop() + this.paginationHeight; let height = $(document).height(); if(pos >= height && !this.doRequest) this.getRecords(); }; public getRecords = () :void =>{ this.doRequest = true; //здесь this - ссылка на Pagination return new Request({ url: this.url + this.currentPage, type: `GET`, dataType: this.requestDataType, onRequest: (data) =>{ //а здесь this превратился в объект $.ajax() this.doRequest = false; this.currentPage++; this.onRecordsLoaded(data); } }).exec(); }; } The question is how to throw this from Pagination to onRequest so that inside the onRequest Holy Island islands of Pagination ?
ps
var _self = this; course, you can create a variable like before before new Request() like var _self = this; and further in onRequest pull it, but it will be a crutch pulling approach used in es5. This is not an option, because I want to make everything beautiful.
pps
So far, I have done a context transfer, passing this in getRecords ,
public getRecords = (context: Pagination) :void =>{ context.doRequest = true; return new Request({ url: context.url + context.currentPage, type: `GET`, dataType: context.requestDataType, onRequest: (data) =>{ context.doRequest = false; context.currentPage++; context.onRecordsLoaded(context.context, data); } }).exec(); }; And in the constructor, in the same way, remember the class context from which the Pagination instance was created:
constructor (context :Object, params? :Object){ this.context = context;