finnsson / pagerjs

The Single Page Application Framework for KnockoutJS
http://pagerjs.com/
MIT License
271 stars 68 forks source link

Params does not parse arrays correctly #222

Closed TheHans255 closed 7 years ago

TheHans255 commented 8 years ago

I have a page in my application with this link:

<a data-bind="page-href: {path: '/graph', params: {id: data.entity.id, relationshipTypes: [3,4,6]}}">
    Test
</a>

that leads to a page like this one:

<article id="graph-page"
     data-bind="page: { 
        id: 'graph', 
        params: { 'id': '', 'relationshipTypes': [] } }"
     >
    <span data-bind="text: relationshipTypes"></span>
</article>

The link URL that is produced is serialized to

http://<domain>/#/graph?id=195975749&relationshipTypes%5B%5D=1&relationshipTypes%5B%5D=2&relationshipTypes%5B%5D=3&relationshipTypes%5B%5D=4&relationshipTypes%5B%5D=5&relationshipTypes%5B%5D=6

which becomes

http://<domain>/#/graph?id=195975749&relationshipTypes[]=1&relationshipTypes[]=2&relationshipTypes[]=3&relationshipTypes[]=4&relationshipTypes[]=5&relationshipTypes[]=6

This appears to be within jQuery's spec, and I would agree that it is a valid URL string to use.

However, PagerJS does not know how to parse this - relationshipTypes is left unbound in the page's view model. Would we be able to add the ability to parse these sorts of arguments into PagerJS?

For reference, this appears to be the function we would need to augment - it appears to do a simple Regex match that only recognizes primitive values as parameters:

var parseStringAsParameters = function (query) {
    var match,
        urlParams = {},
        search = /([^&=]+)=?([^&]*)/g;

    while (match = search.exec(query)) {
        urlParams[match[1]] = match[2];
    }
    return urlParams;
};
TheHans255 commented 8 years ago

This issue has been fixed in this fork:

https://github.com/rocketmonkeys/pagerjs

DKhalil commented 7 years ago

I just merged the pullrequest from that repo!