Skip to content

Instantly share code, notes, and snippets.

@lholmquist
Last active December 12, 2015 00:28
Show Gist options
  • Save lholmquist/4683988 to your computer and use it in GitHub Desktop.
Save lholmquist/4683988 to your computer and use it in GitHub Desktop.
js github paging

I was trying out the js paging stuff with github and i noticed something that i think is a bug.

Here is the pipeline/pipe setup

var pipeline = AeroGear.Pipeline(
    {
        name: "githubcommits",
        settings: {
            baseURL: "https://api.github.com/repos/lholmquist/aerogear-js/",
            endpoint: "commits",
            pageConfig: {
                //previousIdentifier: "first"
                nextIdentifier: "next"
            }
        }
    }
);


var gCommit = pipeline.pipes.githubcommits;

gCommit.read({
    query:{
        per_page: 3
    },
    success: function( pagedResult ) {
        spitOut( pagedResult ); //loops through and outputs the records to the console
        pagedResult.next({
            success: function( data ) {
                spitOut( data );
            }
        });
    },
    error: function( error ) {
        console.log( error );
    }
});

Github's "prev" identifier is actually "first", so if i don't specify it, the next funciton and the previous function that get returned with the paged result are actually the same

webLinkingPageParser , commented where the issue is

var linkAr, linksAr, currentLink, params, paramAr, identifier,
            query = {};

        linksAr = jqXHR.getResponseHeader( "Link" ).split( "," );
        for ( var link in linksAr ) {
            linkAr = linksAr[ link ].trim().split( ";" );
            for ( var item in linkAr ) {
                currentLink = linkAr[ item ].trim();
                if ( currentLink.indexOf( "<" ) === 0 && currentLink.lastIndexOf( ">" ) === linkAr[ item ].length - 1 ) {
                    params = currentLink.substr( 1, currentLink.length - 2 ).split( "?" )[ 1 ];
                } else if ( currentLink.indexOf( "rel=" ) === 0 ) {
                //Once it gets into here, it finds the "next" identifier ok,  
                //but since identifier never gets reset
                //after it has been added to the query object below, if it fails both conditionals, 
                //then the previous thing is overritten in the query object below
                    if ( currentLink.indexOf( pageConfig.previousIdentifier ) >= 0 ) {
                        identifier = pageConfig.previousIdentifier;
                    } else if ( currentLink.indexOf( pageConfig.nextIdentifier ) >= 0 ) {
                        identifier = pageConfig.nextIdentifier;
                    }
                }
            }

            query[ identifier ] = params;
        }

        return query;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment