Skip to content

Instantly share code, notes, and snippets.

@juuliaans
Last active December 19, 2015 23:29
Show Gist options
  • Select an option

  • Save juuliaans/6034831 to your computer and use it in GitHub Desktop.

Select an option

Save juuliaans/6034831 to your computer and use it in GitHub Desktop.

Revisions

  1. juuliaans revised this gist Jul 19, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backbone-route-builder.js
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ resolveURL: function (route, params, appendRoot) {
    //matchs named parameters
    var regExpNamedParams = new RegExp("\\((/([^\\/]+):"+p+")\\)");

    //this is if the parameters has any special way of encoding itself (such as a date could be enconded as text yy-mm-dd)
    //this is if the parameter has any special way of encoding itself (such as a date could be enconded as text yy-mm-dd)
    //var value = router.encodeParameter(p, params[p]);

    var nameOptParam = href.match(regExpNamedParams);
  2. juuliaans created this gist Jul 19, 2013.
    38 changes: 38 additions & 0 deletions backbone-route-builder.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    resolveURL: function (route, params, appendRoot) {

    var appendRoot = (appendRoot == false) ? false : true;
    var route, params;
    var href = "";

    var regExpAllNormalParams = /(\(\?)?\/:\w+/g;
    var regExpAllOptionalParams = /\((.*?)\)/g;

    href = router.routes[route];
    for (var p in params) {
    //matchs normal parameters
    var regExpNormal = new RegExp("(\\(\\?)?:"+ p);

    //matchs optional parameters
    var regExpOptional = new RegExp("\\((/:" + p + ")\\)");

    //matchs named parameters
    var regExpNamedParams = new RegExp("\\((/([^\\/]+):"+p+")\\)");

    //this is if the parameters has any special way of encoding itself (such as a date could be enconded as text yy-mm-dd)
    //var value = router.encodeParameter(p, params[p]);

    var nameOptParam = href.match(regExpNamedParams);
    if (nameOptParam != null) {
    href = href.replace(regExpNamedParams, "/" + ((nameOptParam[2]) ? nameOptParam[2] : "") + escape(value));
    }
    href = href.replace(regExpOptional, "/" + escape(value));
    href = href.replace(regExpNormal, escape(value));
    }

    href = href.replace(regExpAllOptionalParams, "");
    href = href.replace(regExpAllNormalParams, "");
    //if the app has any root url
    //href = (appendRoot) ? app.root + href : href;

    return href;
    }