can-route
Advanced tools
Comparing version 4.0.0-pre.15 to 4.0.0-pre.16
@@ -33,2 +33,5 @@ /*jshint -W079 */ | ||
function canRoute(url, defaults){ | ||
//!steal-remove-start | ||
devLog.warn('Call route.register(url,defaults) instead of calling route(url, defaults)'); | ||
//!steal-remove-end | ||
registerRoute.register(url, defaults); | ||
@@ -46,5 +49,5 @@ return canRoute; | ||
// A dummy events object used to dispatch url change events on. | ||
var matchedObservable = new Observation(function canRoute_matchedRoute(){ | ||
var currentRuleObservable = new Observation(function canRoute_matchedRoute(){ | ||
var url = bindingProxy.call("can.getValue"); | ||
return canRoute.deparam(url).route; | ||
return canRoute.rule(url); | ||
}); | ||
@@ -63,3 +66,3 @@ | ||
var serialized = canReflect.serialize( canRoute.data ), | ||
currentRouteName = matchedObservable.get(), | ||
currentRouteName = currentRuleObservable.get(), | ||
route = routeParam.getMatchedRoute(serialized, currentRouteName), | ||
@@ -79,4 +82,2 @@ path = routeParam.paramFromRoute(route, serialized); | ||
// Deparameterizes the portion of the hash of interest and assign the | ||
@@ -165,35 +166,3 @@ // values to the `route.data` removing existing values no longer in the hash. | ||
/** | ||
* @function can-route.start start | ||
* @parent can-route.static | ||
* @release 3.3 | ||
* | ||
* Initializes can-route. | ||
* | ||
* @signature `route.start()` | ||
* | ||
* Sets up the two-way binding between the hash and the can-route observable | ||
* map and sets the route map to its initial values. | ||
* | ||
* ```js | ||
* route("{page}", { page: "home" })); | ||
* | ||
* route.start(); | ||
* route.data.page; // -> "home" | ||
* ``` | ||
* | ||
* @return {can-route} The can-route object. | ||
* | ||
* @body | ||
* | ||
* ## Use | ||
* | ||
* After setting all your routes, call `route.start()`. | ||
* | ||
* ```js | ||
* route("overview/{dateStart}-{dateEnd}"); | ||
* route("{type}/{id}"); | ||
* route.start(); | ||
* ``` | ||
*/ | ||
start: function (val) { | ||
@@ -220,3 +189,3 @@ if (val !== true) { | ||
link: urlHelpers.link, | ||
current: urlHelpers.current, | ||
isCurrent: urlHelpers.isCurrent, | ||
bindings: bindingProxy.bindings, | ||
@@ -250,26 +219,14 @@ | ||
}, | ||
/** | ||
* @function can-route.matched matched | ||
* @parent can-route.static | ||
* @description A compute representing the currently matched route. | ||
* @signature `route.matched()` | ||
* @return {String} The currently matched route. | ||
* | ||
* @body | ||
* Use `route.matched()` to find the currently matched route. | ||
* | ||
* ```js | ||
* route("{type}", { type: "foo" }); | ||
* route("{type}/{subtype}"); | ||
* | ||
* route.matched(); // "{type}" | ||
* | ||
* route.data.subtype = "foo"; | ||
* | ||
* route.matched(); // "{type}/{subtype}" | ||
* ``` | ||
*/ | ||
matched: makeCompute( matchedObservable ) | ||
currentRule: makeCompute( currentRuleObservable ), | ||
register: registerRoute.register, | ||
rule: function(url){ | ||
var rule = routeDeparam.getRule(url); | ||
if(rule) { | ||
return rule.route; | ||
} | ||
} | ||
}); | ||
// The functions in the following list applied to `canRoute` (e.g. `canRoute.attr('...')`) will | ||
@@ -377,2 +334,3 @@ // instead act on the `canRoute.data` observe. | ||
var viewModelSymbol = canSymbol.for("can.viewModel"); | ||
Object.defineProperty(canRoute,"data", { | ||
@@ -390,2 +348,5 @@ get: function(){ | ||
} | ||
if(data && data[viewModelSymbol] !== undefined) { | ||
data = data[viewModelSymbol]; | ||
} | ||
// if it’s a map, we make it always set strings for backwards compat | ||
@@ -422,2 +383,6 @@ if( "attr" in data ) { | ||
// LEGACY | ||
canRoute.matched = canRoute.currentRule; | ||
canRoute.current = canRoute.isCurrent; | ||
module.exports = namespace.route = canRoute; |
{ | ||
"name": "can-route", | ||
"version": "4.0.0-pre.15", | ||
"version": "4.0.0-pre.16", | ||
"description": "Observable front-end application routing for CanJS.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://canjs.com/doc/can-route.html", |
var deparam = require('can-deparam'); | ||
var each = require('can-util/js/each/each'); | ||
var canReflect = require("can-reflect"); | ||
var deepAssign = require('can-util/js/deep-assign/deep-assign'); | ||
@@ -16,2 +16,26 @@ | ||
function canRoute_getRule(url){ | ||
var root =bindingProxy.call("root"); | ||
if (root.lastIndexOf("/") === root.length - 1 && | ||
url.indexOf("/") === 0) { | ||
url = url.substr(1); | ||
} | ||
// See if the url matches any routes by testing it against the `route.test` `RegExp`. | ||
// By comparing the URL length the most specialized route that matches is used. | ||
var route = { | ||
length: -1 | ||
}; | ||
canReflect.eachKey(register.routes, function (temp, name) { | ||
if (temp.test.test(url) && temp.length > route.length) { | ||
route = temp; | ||
} | ||
}); | ||
// If a route was matched. | ||
if (route.length > -1) { | ||
return route; | ||
} | ||
} | ||
/** | ||
@@ -69,26 +93,9 @@ * @function can-route.deparam deparam | ||
*/ | ||
module.exports = function canRoute_deparam(url) { | ||
function canRoute_deparam(url) { | ||
// remove the url | ||
var root =bindingProxy.call("root"); | ||
if (root.lastIndexOf("/") === root.length - 1 && | ||
url.indexOf("/") === 0) { | ||
url = url.substr(1); | ||
} | ||
// See if the url matches any routes by testing it against the `route.test` `RegExp`. | ||
// By comparing the URL length the most specialized route that matches is used. | ||
var route = { | ||
length: -1 | ||
}, | ||
querySeparator =bindingProxy.call("querySeparator"), | ||
paramsMatcher =bindingProxy.call("paramsMatcher"); | ||
each(register.routes, function (temp, name) { | ||
if (temp.test.test(url) && temp.length > route.length) { | ||
route = temp; | ||
} | ||
}); | ||
var route = canRoute_getRule(url), | ||
querySeparator =bindingProxy.call("querySeparator"), | ||
paramsMatcher =bindingProxy.call("paramsMatcher"); | ||
// If a route was matched. | ||
if (route.length > -1) { | ||
if (route) { | ||
@@ -109,3 +116,3 @@ var // Since `RegExp` backreferences are used in `route.test` (parens) | ||
// parts if that part is not empty. | ||
each(parts, function (part, i) { | ||
parts.forEach(function (part, i) { | ||
if (part && part !== querySeparator) { | ||
@@ -115,3 +122,2 @@ obj[route.names[i]] = decode(part); | ||
}); | ||
obj.route = route.route; | ||
return obj; | ||
@@ -124,2 +130,7 @@ } | ||
return paramsMatcher.test(url) ? deparam(url.slice(1)) : {}; | ||
}; | ||
} | ||
canRoute_deparam.getRule = canRoute_getRule; | ||
module.exports = canRoute_deparam; |
@@ -140,3 +140,3 @@ var bindingProxy = require("./binding-proxy"); | ||
/** | ||
* @function can-route.current current | ||
* @function can-route.isCurrent isCurrent | ||
* @parent can-route.static | ||
@@ -146,3 +146,3 @@ * | ||
* | ||
* @signature `route.current(data [,subsetMatch] )` | ||
* @signature `route.isCurrent(data [,subsetMatch] )` | ||
* | ||
@@ -155,4 +155,4 @@ * Compares `data` to the current route. Used to verify if an object is | ||
* | ||
* route.current({page: "recipes"}); //-> false | ||
* route.current({page: "recipes"}, true); //-> true | ||
* route.isCurrent({page: "recipes"}); //-> false | ||
* route.isCurrent({page: "recipes"}, true); //-> true | ||
* ``` | ||
@@ -178,11 +178,11 @@ * | ||
* route.data.id = 5; // location.hash -> "#!id=5" | ||
* route.current({ id: 5 }); // -> true | ||
* route.current({ id: 5, type: 'videos' }); // -> false | ||
* route.isCurrent({ id: 5 }); // -> true | ||
* route.isCurrent({ id: 5, type: 'videos' }); // -> false | ||
* | ||
* route.data.type = 'videos'; | ||
* // location.hash -> #!id=5&type=videos | ||
* route.current({ id: 5, type: 'videos' }); // -> true | ||
* route.isCurrent({ id: 5, type: 'videos' }); // -> true | ||
* ``` | ||
*/ | ||
current: function canRoute_current(options, subsetMatch) { | ||
isCurrent: function canRoute_isCurrent(options, subsetMatch) { | ||
if(subsetMatch) { | ||
@@ -189,0 +189,0 @@ // everything in options shouhld be in baseOptions |
@@ -12,5 +12,5 @@ var canRoute = require('can-route'); | ||
test("deparam", function () { | ||
test("deparam and rule", function () { | ||
canRoute("{page}", { | ||
canRoute.register("{page}", { | ||
page: "index" | ||
@@ -21,11 +21,11 @@ }); | ||
deepEqual(obj, { | ||
page: "can.Control", | ||
route: "{page}" | ||
page: "can.Control" | ||
}); | ||
QUnit.equal(canRoute.rule("can.Control"), "{page}"); | ||
obj = canRoute.deparam(""); | ||
deepEqual(obj, { | ||
page: "index", | ||
route: "{page}" | ||
page: "index" | ||
}); | ||
QUnit.equal(canRoute.rule(""), "{page}"); | ||
@@ -35,8 +35,9 @@ obj = canRoute.deparam("can.Control&where=there"); | ||
page: "can.Control", | ||
where: "there", | ||
route: "{page}" | ||
where: "there" | ||
}); | ||
QUnit.equal(canRoute.rule("can.Control&where=there"), "{page}"); | ||
canRoute.routes = {}; | ||
canRoute("{page}/{index}", { | ||
canRoute.register("{page}/{index}", { | ||
page: "index", | ||
@@ -50,5 +51,6 @@ index: "foo" | ||
index: "foo", | ||
where: "there", | ||
route: "{page}/{index}" | ||
where: "there" | ||
}, "default value and queryparams"); | ||
QUnit.equal(canRoute.rule("can.Control/&where=there"), "{page}/{index}"); | ||
}); | ||
@@ -58,3 +60,3 @@ | ||
var obj; | ||
canRoute("pages/{var1}/{var2}/{var3}", { | ||
canRoute.register("pages/{var1}/{var2}/{var3}", { | ||
var1: 'default1', | ||
@@ -76,5 +78,6 @@ var2: 'default2', | ||
var2: 'val2', | ||
var3: 'val3', | ||
route: "pages/{var1}/{var2}/{var3}" | ||
var3: 'val3' | ||
}); | ||
QUnit.equal(canRoute.rule("pages/val1/val2/val3&invalid-parameters"), "pages/{var1}/{var2}/{var3}"); | ||
}); | ||
@@ -97,3 +100,3 @@ | ||
test("param", function () { | ||
canRoute("pages/{page}", { | ||
canRoute.register("pages/{page}", { | ||
page: "index" | ||
@@ -113,3 +116,3 @@ }) | ||
canRoute("pages/{page}/{foo}", { | ||
canRoute.register("pages/{page}/{foo}", { | ||
page: "index", | ||
@@ -145,3 +148,3 @@ foo: "bar" | ||
canRoute.routes = {}; | ||
canRoute("pages/:page", { | ||
canRoute.register("pages/:page", { | ||
page: "index" | ||
@@ -161,3 +164,3 @@ }) | ||
canRoute("pages/:page/:foo", { | ||
canRoute.register("pages/:page/:foo", { | ||
page: "index", | ||
@@ -208,3 +211,3 @@ foo: "bar" | ||
test("light param", function () { | ||
canRoute("{page}", { | ||
canRoute.register("{page}", { | ||
page: "index" | ||
@@ -218,3 +221,3 @@ }) | ||
canRoute("pages/{p1}/{p2}/{p3}", { | ||
canRoute.register("pages/{p1}/{p2}/{p3}", { | ||
p1: "index", | ||
@@ -242,3 +245,3 @@ p2: "foo", | ||
canRoute("pages/{p1}", { | ||
canRoute.register("pages/{p1}", { | ||
p2: "foo" | ||
@@ -255,3 +258,3 @@ }) | ||
canRoute("{page}/{type}", { | ||
canRoute.register("{page}/{type}", { | ||
page: "index", | ||
@@ -315,3 +318,3 @@ type: "foo" | ||
canRoute.routes = {}; | ||
canRoute("{foo}/{bar}", { | ||
canRoute.register("{foo}/{bar}", { | ||
foo: 1, | ||
@@ -329,18 +332,18 @@ bar: 2 | ||
foo: 1, | ||
bar: 2, | ||
route: "{foo}/{bar}" | ||
}) | ||
bar: 2 | ||
}); | ||
QUnit.equal(canRoute.rule("/"), "{foo}/{bar}"); | ||
}); | ||
test("precident", function () { | ||
canRoute("{who}", { | ||
canRoute.register("{who}", { | ||
who: "index" | ||
}); | ||
canRoute("search/{search}"); | ||
canRoute.register("search/{search}"); | ||
var obj = canRoute.deparam("can.Control"); | ||
deepEqual(obj, { | ||
who: "can.Control", | ||
route: "{who}" | ||
who: "can.Control" | ||
}); | ||
QUnit.equal(canRoute.rule("can.Control"), "{who}"); | ||
@@ -350,5 +353,6 @@ obj = canRoute.deparam("search/can.Control"); | ||
search: "can.Control", | ||
route: "search/{search}" | ||
}, "bad deparam"); | ||
QUnit.equal(canRoute.rule("search/can.Control"), "search/{search}"); | ||
equal(canRoute.param({ | ||
@@ -366,6 +370,6 @@ search: "can.Control" | ||
test("better matching precident", function () { | ||
canRoute("{type}", { | ||
canRoute.register("{type}", { | ||
who: "index" | ||
}); | ||
canRoute("{type}/{id}"); | ||
canRoute.register("{type}/{id}"); | ||
@@ -380,4 +384,4 @@ equal(canRoute.param({ | ||
test("param with currentRoute name", function () { | ||
canRoute("holler") | ||
canRoute("foo"); | ||
canRoute.register("holler") | ||
canRoute.register("foo"); | ||
@@ -393,6 +397,6 @@ var res = canRoute.param({ | ||
canRoute.routes = {}; | ||
canRoute("foo", { | ||
canRoute.register("foo", { | ||
foo: true | ||
}); | ||
canRoute("food", { | ||
canRoute.register("food", { | ||
food: true | ||
@@ -407,3 +411,3 @@ }); | ||
test("strange characters", function () { | ||
canRoute("{type}/{id}"); | ||
canRoute.register("{type}/{id}"); | ||
var res = canRoute.deparam("foo/" + encodeURIComponent("\/")) | ||
@@ -419,4 +423,4 @@ equal(res.id, "\/") | ||
test("empty default is matched even if last", function () { | ||
canRoute("{who}"); | ||
canRoute("", { | ||
canRoute.register("{who}"); | ||
canRoute.register("", { | ||
foo: "bar" | ||
@@ -427,23 +431,25 @@ }) | ||
deepEqual(obj, { | ||
foo: "bar", | ||
route: "" | ||
foo: "bar" | ||
}); | ||
QUnit.equal(canRoute.rule(""), ""); | ||
}); | ||
test("order matched", function () { | ||
canRoute("{foo}"); | ||
canRoute("{bar}") | ||
canRoute.register("{foo}"); | ||
canRoute.register("{bar}") | ||
var obj = canRoute.deparam("abc"); | ||
deepEqual(obj, { | ||
foo: "abc", | ||
route: "{foo}" | ||
foo: "abc" | ||
}); | ||
QUnit.equal(canRoute.rule("abc"), "{foo}"); | ||
}); | ||
test("param order matching", function () { | ||
canRoute("", { | ||
canRoute.register("", { | ||
bar: "foo" | ||
}); | ||
canRoute("something/{bar}"); | ||
canRoute.register("something/{bar}"); | ||
var res = canRoute.param({ | ||
@@ -457,3 +463,3 @@ bar: "foo" | ||
canRoute("{recipe}", { | ||
canRoute.register("{recipe}", { | ||
recipe: "recipe1", | ||
@@ -463,3 +469,3 @@ task: "task3" | ||
canRoute("{recipe}/{task}", { | ||
canRoute.register("{recipe}/{task}", { | ||
recipe: "recipe1", | ||
@@ -484,3 +490,3 @@ task: "task3" | ||
test("dashes in routes", function () { | ||
canRoute("{foo}-{bar}"); | ||
canRoute.register("{foo}-{bar}"); | ||
@@ -490,4 +496,3 @@ var obj = canRoute.deparam("abc-def"); | ||
foo: "abc", | ||
bar: "def", | ||
route: "{foo}-{bar}" | ||
bar: "def" | ||
}); | ||
@@ -506,4 +511,4 @@ }); | ||
canRoute("{page}/{subpage}"); | ||
canRoute("foo/{page}/{subpage}"); | ||
canRoute.register("{page}/{subpage}"); | ||
canRoute.register("foo/{page}/{subpage}"); | ||
@@ -520,4 +525,4 @@ dev.warn = oldlog; | ||
canRoute("foo/{page}/{subpage}"); | ||
canRoute("{page}/{subpage}"); | ||
canRoute.register("foo/{page}/{subpage}"); | ||
canRoute.register("{page}/{subpage}"); | ||
@@ -535,4 +540,4 @@ dev.warn = oldlog; | ||
canRoute("login", { "page": "auth", "subpage": "login" }); | ||
canRoute("signup", { "page": "auth", "subpage": "signup" }); | ||
canRoute.register("login", { "page": "auth", "subpage": "login" }); | ||
canRoute.register("signup", { "page": "auth", "subpage": "signup" }); | ||
@@ -555,12 +560,12 @@ dev.warn = oldlog; | ||
//should warn | ||
canRoute("login", { "page":"auth" }); | ||
canRoute("signup", { "page":"auth" }); | ||
canRoute.register("login", { "page":"auth" }); | ||
canRoute.register("signup", { "page":"auth" }); | ||
//should not warn | ||
canRoute("login2/", { "page":"auth2" }); | ||
canRoute("login2", { "page":"auth2" }); | ||
canRoute.register("login2/", { "page":"auth2" }); | ||
canRoute.register("login2", { "page":"auth2" }); | ||
//should not warn | ||
canRoute("login3", { "page":"auth3" }); | ||
canRoute("login3", { "page":"auth3" }); | ||
canRoute.register("login3", { "page":"auth3" }); | ||
canRoute.register("login3", { "page":"auth3" }); | ||
@@ -567,0 +572,0 @@ dev.warn = oldlog; |
@@ -252,3 +252,3 @@ /* jshint asi:true */ | ||
var isOnTestPage = new win.Observation(function(){ | ||
return iCanRoute.current({page: "test"}); | ||
return iCanRoute.isCurrent({page: "test"}); | ||
}); | ||
@@ -260,3 +260,3 @@ | ||
equal(iCanRoute.current({page: "test"}), false, "initially not on test page") | ||
equal(iCanRoute.isCurrent({page: "test"}), false, "initially not on test page") | ||
setTimeout(function(){ | ||
@@ -412,4 +412,3 @@ iCanRoute.attr("page","test"); | ||
deepEqual(obj, { | ||
page: "can.Control", | ||
route: "{page}\\.html" | ||
page: "can.Control" | ||
}); | ||
@@ -416,0 +415,0 @@ |
@@ -360,4 +360,3 @@ /* jshint asi:true */ | ||
deepEqual(obj, { | ||
page: "can.Control", | ||
route: "{page}\\.html" | ||
page: "can.Control" | ||
}); | ||
@@ -364,0 +363,0 @@ |
@@ -28,3 +28,3 @@ var canRoute = require('can-route'); | ||
//queues.log("flush"); | ||
test("matched() compute", function() { | ||
test("currentRule() compute", function() { | ||
@@ -43,11 +43,11 @@ mockRoute.start(); | ||
canRoute.data = appState; | ||
canRoute("{type}", { type: "foo" }); | ||
canRoute("{type}/{subtype}"); | ||
canRoute.register("{type}", { type: "foo" }); | ||
canRoute.register("{type}/{subtype}"); | ||
canRoute.start(); | ||
equal(appState.route, undefined, "should not set route on appState"); | ||
equal(canRoute.matched(), "{type}", "should set route.matched property"); | ||
equal(canRoute.currentRule(), "{type}", "should set route.currentRule property"); | ||
appState.subtype = "bar"; | ||
var check = function(){ | ||
if(canRoute.matched() === "{type}/{subtype}") { | ||
if(canRoute.currentRule() === "{type}/{subtype}") { | ||
QUnit.ok(true, "moved to right route"); | ||
@@ -54,0 +54,0 @@ mockRoute.stop(); |
require("./param-deparam-test"); | ||
require("./link-to-test"); | ||
require("./url-test"); | ||
require("./route-data-test"); | ||
require("./route-observability-test"); | ||
@@ -5,0 +6,0 @@ require("./route-observe-test"); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104919
36
2459