From 834dd0261043db05f3071bcceac510646cb3dca8 Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Wed, 11 Jan 2017 14:23:42 -0800 Subject: [PATCH] feat: bring back m.route.params() Because it's annoying only having route params available to the top-level components within a route. --- api/router.js | 4 ++++ api/tests/test-router.js | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/api/router.js b/api/router.js index cf406737..c25ac257 100644 --- a/api/router.js +++ b/api/router.js @@ -54,6 +54,10 @@ module.exports = function($window, redrawService) { route.set(href, undefined, undefined) } } + route.params = function(key) { + if(typeof attrs !== "undefined" && typeof key !== "undefined") return attrs[key] + return attrs + } return route } diff --git a/api/tests/test-router.js b/api/tests/test-router.js index 6a281237..f97fc5db 100644 --- a/api/tests/test-router.js +++ b/api/tests/test-router.js @@ -1194,6 +1194,27 @@ o.spec("route", function() { done() }, FRAME_BUDGET * 2) }) + + o("m.route.params is available outside of route handlers", function(done) { + $window.location.href = prefix + "/" + + route(root, "/1", { + "/:id" : { + view : function() { + o(route.params("id")).equals("1") + + return m("div") + } + } + }) + + callAsync(function() { + o(route.params("id")).equals("1") + o(route.params()).deepEquals({id:"1"}) + + done() + }) + }) }) }) })