diff --git a/backbone.paginator/backbone.paginator-tests.ts b/backbone.paginator/backbone.paginator-tests.ts new file mode 100644 index 000000000..413c4c373 --- /dev/null +++ b/backbone.paginator/backbone.paginator-tests.ts @@ -0,0 +1,305 @@ +/// +/// +/// + +module BackbonePaginatorTests { + + class TestModel extends Backbone.Model{}; + + var makeFetchOptions = >() => { + return { + reset: true, + url: 'example.com', + beforeSend: (jqxhr: JQueryXHR) => {}, + success: (model: TestModel, response: any, options: any) => {}, + error: (collection: TCol, jqxhr: JQueryXHR, options: any) => {}, + parse: '', + }; + }; + + + module InitializingWithNoOption { + + class TestCollection extends Backbone.PageableCollection { + constructor(){ + super(); + } + } + + var testCollection = new TestCollection(); + + } + + + + module InitializingWithOptions { + + class TestCollection extends Backbone.PageableCollection { + + constructor(models?: TestModel[], + options?: Backbone.PageableInitialOptions){ + super(); + } + + } + + var testCollection1 = new TestCollection(); + + var testCollection2 = new TestCollection([ + new TestModel(), + new TestModel() + ]); + + var testCollection3 = new TestCollection([], {}); + + var testCollection4 = new TestCollection([],{ + comparator: ()=>1, + full: true, + state: {}, + queryParam: {}, + }); + + var testCollection5 = new TestCollection([],{ + state: { + firstPage: 0, + lastPage: 0, + currentPage: 0, + pageSize: 1, + totalPages: 1, + totalRecords: 1, + sortKey: 'id', + order: 1, + }, + queryParam: { + currentPage: 'current_page', + pageSize: 'page_size', + totalPages: 'total_pages', + totalRecords: 'total_records', + sortKey: 'sort_key', + order: 'order', + directions: '', + }, + }); + + var testCollection6 = new TestCollection([ + {}, + {}, + ]); + + } + + + + module Fetching { + + class TestCollection extends Backbone.PageableCollection { + constructor(models?: TestModel[], + options?: Backbone.PageableInitialOptions){ + super(); + } + } + + + var testCollection = new TestCollection(); + + var result:JQueryXHR = testCollection.fetch(); + + testCollection.fetch({}); + + testCollection.fetch(makeFetchOptions()); + + } + + + + module Paging { + + class TestCollection extends Backbone.PageableCollection { + constructor(models?: TestModel[], + options?: Backbone.PageableInitialOptions){ + super(); + } + } + + var options = makeFetchOptions(); + + var testCollection = new TestCollection(); + + + var result:JQueryXHR|TestCollection = testCollection.getFirstPage(); + + testCollection.getFirstPage(options); + + // 'silent's type is boolean. (structural subtyping) + testCollection.getFirstPage({silent: 'aa'}); + // 'url's type is string. (structural subtyping) + testCollection.getFirstPage({url: true}); + + + result = testCollection.getLastPage(); + + testCollection.getLastPage(options); + + // 'silent's type is boolean. (structural subtyping) + testCollection.getLastPage({silent: 'aa'}); + // 'url's type is string. (structural subtyping) + testCollection.getLastPage({url: true}); + + + result = testCollection.getNextPage(); + + testCollection.getNextPage(options); + + // 'silent's type is boolean. (structural subtyping) + testCollection.getNextPage({silent: 'aa'}); + // 'url's type is string. (structural subtyping) + testCollection.getNextPage({url: true}); + + + result = testCollection.getPage(1); + + testCollection.getPage("1", options); + + // 'silent's type is boolean. (structural subtyping) + testCollection.getPage(1, {silent: 'aa'}); + // 'url's type is string. (structural subtyping) + testCollection.getPage(1, {url: true}); + + + result = testCollection.getPageByOffset(1); + + testCollection.getPageByOffset(1, options); + + // 'silent's type is boolean. (structural subtyping) + testCollection.getPageByOffset(1, {silent: 'aa'}); + // 'url's type is string. (structural subtyping) + testCollection.getPageByOffset(1, {url: true}); + + + result = testCollection.getPreviousPage(); + + testCollection.getPreviousPage(options); + + // 'silent's type is boolean. (structural subtyping) + testCollection.getPreviousPage({silent: 'aa'}); + // 'url's type is string. (structural subtyping) + testCollection.getPreviousPage({url: true}); + + + var hasPage:boolean = testCollection.hasNextPage(); + + hasPage = testCollection.hasPreviousPage(); + + } + + + + + module Parse { + + class TestCollection extends Backbone.PageableCollection { + constructor(models?: TestModel[], + options?: Backbone.PageableInitialOptions){ + super(); + } + } + + var testCollection = new TestCollection(); + + var result:any[] = testCollection.parse({}, {}); + + + var resultLinks:any = testCollection.parseLinks({}, {}); + + resultLinks = testCollection.parseLinks({}, { xhr: $.ajax({}) } ); + + + result = testCollection.parseRecords({}, {}); + + + var resultState: Backbone.PageableState = testCollection.parseState( + {}, + { + currentPage: 'current_page', + pageSize: 'page_size', + totalPages: 'total_pages', + totalRecords: 'total_records', + sortKey: 'sort_key', + order: 'order', + directions: '', + }, + { + firstPage: 0, + lastPage: 0, + currentPage: 0, + pageSize: 1, + totalPages: 1, + totalRecords: 1, + sortKey: 'id', + order: 1, + }, + {}); + + } + + + + module Setting { + + class TestCollection extends Backbone.PageableCollection { + constructor(models?: TestModel[], + options?: Backbone.PageableInitialOptions){ + super(); + } + } + + var testCollection = new TestCollection(); + + var options = makeFetchOptions(); + + + var result1:JQueryXHR|TestCollection + = testCollection.setPageSize(1, options); + + + var result2:TestCollection + = testCollection.setSorting('id', 1, options); + + + result1 = testCollection.switchMode( + 'server', + {fetch: true, resetState: true} + ); + + } + + + + module Syncing { + + class TestCollection extends Backbone.PageableCollection { + constructor(models?: TestModel[], + options?: Backbone.PageableInitialOptions){ + super(); + } + } + + var testCollection = new TestCollection(); + + + var result:JQueryXHR = testCollection.sync('server', new TestModel(), {}); + + result = testCollection.sync('server', testCollection, {}); + + } + + + + module Confllict { + + var result:typeof Backbone.PageableCollection + = Backbone.PageableCollection.noConflict(); + + } + +} diff --git a/backbone.paginator/backbone.paginator.d.ts b/backbone.paginator/backbone.paginator.d.ts new file mode 100644 index 000000000..6eb560cc8 --- /dev/null +++ b/backbone.paginator/backbone.paginator.d.ts @@ -0,0 +1,116 @@ +// Type definitions for backbone.paginator 2.0.2 +// Project: https://github.com/backbone-paginator/backbone.paginator +// Definitions by: Nyamazing +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module Backbone { + + interface PageableState { + firstPage?: number; + lastPage?: number; + currentPage?: number; + pageSize?: number; + totalPages?: number; + totalRecords?: number; + sortKey?: string; + order?: number; + } + + interface PageableQueryParams { + currentPage?: string; + pageSize?: string; + totalPages?: string; + totalRecords?: string; + sortKey?: string; + order?: string; + directions?: any; + } + + interface PageableInitialOptions { + comparator?: (...options: any[]) => number; + full?: boolean; + state?: PageableState; + queryParam?: PageableQueryParams; + } + + interface PageableParseLinksOptions { + xhr?: JQueryXHR; + } + + interface PageableSetSortingOptions { + side?: string; + full?: boolean; + sortValue?: (model: TModel, sortKey: string) => any | string; + } + + interface PageableSwitchModeOptions { + fetch?: boolean; + resetState?: boolean; + } + + type PageableGetPageOptions = CollectionFetchOptions|Silenceable; + + class PageableCollection extends Collection{ + + fullCollection: Collection; + mode: string; + queryParams: PageableQueryParams; + state: PageableState; + + constructor(models?: TModel[], options?: PageableInitialOptions); + + fetch(options?: CollectionFetchOptions): JQueryXHR; + + getFirstPage(options?: PageableGetPageOptions): + JQueryXHR|PageableCollection; + + getLastPage(options?: PageableGetPageOptions): + JQueryXHR|PageableCollection; + + getNextPage(options?: PageableGetPageOptions): + JQueryXHR|PageableCollection; + + getPage(index: number|string, options?: PageableGetPageOptions): + JQueryXHR|PageableCollection; + + getPageByOffset(offset: number, options?: PageableGetPageOptions): + JQueryXHR|PageableCollection; + + getPreviousPage(options?: PageableGetPageOptions): + JQueryXHR|PageableCollection; + + hasNextPage(): boolean; + + hasPreviousPage(): boolean; + + parse(resp: any, options?: any): any[]; + + parseLinks(resp: any, options?: PageableParseLinksOptions): any; + + parseRecords(resp: any, options?: any): any[]; + + parseState(resp: any, queryParams: PageableQueryParams, + state: PageableState, options?: any): PageableState; + + setPageSize(pageSize: number, + options?: CollectionFetchOptions): + JQueryXHR|PageableCollection; + + setSorting(sortKey: string, order?: number, + options?: PageableSetSortingOptions): + PageableCollection; + + switchMode(mode?: string, options?: PageableSwitchModeOptions): + JQueryXHR|PageableCollection; + + sync(method: string, + model: TModel|Collection, + options?: any): JQueryXHR; + + static noConflict(): typeof PageableCollection; + + } +} +