Skip to content

Commit 79eb109

Browse files
committed
router subscriber
1 parent 3cc7299 commit 79eb109

File tree

12 files changed

+136
-7
lines changed

12 files changed

+136
-7
lines changed

router.js controller/controller.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
steal.plugins("jquery",
22
"jquery/class",
33
"jquery/controller/history",
4-
"jquery/lang/openajax")
5-
.then("sherpa")
4+
"jquery/lang/openajax",
5+
"ss/router/sherpa")
66
.then(function($) {
77

8-
$.Class.extend("SS.Router",
8+
$.Class.extend("SS.Router.Controller",
99
{
1010
currentParams: {},
1111
URLPrefix: null,

controller/qunit.html

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<html>
2+
<head>
3+
<link rel="stylesheet" type="text/css" href="../../../funcunit/qunit/qunit.css" />
4+
<title>QUnit Test</title>
5+
<style>
6+
body {
7+
margin: 0px; padding: 0px;
8+
}
9+
</style>
10+
<!--[if IE]>
11+
<script type='text/javascript'
12+
src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
13+
<![endif]-->
14+
<script type='text/javascript'>
15+
steal = {ignoreControllers: true}
16+
</script>
17+
<script type='text/javascript' src='../../../steal/steal.js?steal[app]=ss/router/controller/test/qunit'></script>
18+
</head>
19+
<body>
20+
21+
<h1 id="qunit-header">router Test Suite</h1>
22+
<h2 id="qunit-banner"></h2>
23+
<div id="qunit-testrunner-toolbar"></div>
24+
<h2 id="qunit-userAgent"></h2>
25+
<div id="test-content"></div>
26+
<ol id="qunit-tests"></ol>
27+
<div id="qunit-test-area"></div>
28+
</body>
29+
</html>
File renamed without changes.

controller/test/qunit/qunit.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
steal
2+
.plugins("jquery/controller") //load your app
3+
.plugins("ss/router/controller") //load your app
4+
.plugins("funcunit/qunit") //load qunit
5+
.then("controller_test")

qunit.html sherpa/qunit.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html>
22
<head>
3-
<link rel="stylesheet" type="text/css" href="../../funcunit/qunit/qunit.css" />
3+
<link rel="stylesheet" type="text/css" href="../../../funcunit/qunit/qunit.css" />
44
<title>QUnit Test</title>
55
<style>
66
body {
@@ -14,7 +14,7 @@
1414
<script type='text/javascript'>
1515
steal = {ignoreControllers: true}
1616
</script>
17-
<script type='text/javascript' src='../../steal/steal.js?steal[app]=ss/router/test/qunit'></script>
17+
<script type='text/javascript' src='../../../steal/steal.js?steal[app]=ss/router/sherpa/test/qunit'></script>
1818
</head>
1919
<body>
2020

sherpa.js sherpa/sherpa.js

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
steal
22
.plugins("jquery/controller") //load your app
3-
.plugins("ss/router") //load your app
3+
.plugins("ss/router/sherpa") //load your app
44
.plugins("funcunit/qunit") //load qunit
55
.then("sherpa_test")
6-
.then("router_test")

test/qunit/sherpa_test.js sherpa/test/qunit/sherpa_test.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module("Sherpa Router");
22

3+
var Router = new Sherpa.Router();
34
Router.add("/path").to("basic");
45
Router.add("/withvariable/:something").to("with_var");
56
Router.add("/withmatch/:something", { matchesWith: { something: /\d+/ } }).to("with_match");

subscribe/qunit.html

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<html>
2+
<head>
3+
<link rel="stylesheet" type="text/css" href="../../../funcunit/qunit/qunit.css" />
4+
<title>QUnit Test</title>
5+
<style>
6+
body {
7+
margin: 0px; padding: 0px;
8+
}
9+
</style>
10+
<script type='text/javascript'>
11+
steal = {ignoreControllers: true}
12+
</script>
13+
<script type='text/javascript' src='../../../steal/steal.js?steal[app]=ss/router/subscribe/test/qunit'></script>
14+
</head>
15+
<body>
16+
17+
<h1 id="qunit-header">router subscribe Test Suite</h1>
18+
<h2 id="qunit-banner"></h2>
19+
<div id="qunit-testrunner-toolbar"></div>
20+
<h2 id="qunit-userAgent"></h2>
21+
<div id="test-content"></div>
22+
<ol id="qunit-tests"></ol>
23+
<div id="qunit-test-area"></div>
24+
</body>
25+
</html>

subscribe/subscribe.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
steal.plugins("jquery/controller",
2+
"jquery/controller/history",
3+
"jquery/lang/openajax",
4+
"ss/router/sherpa")
5+
.then(function($) {
6+
7+
var SubscriberRouter = new Sherpa.Router();
8+
var RouteToCallback = {};
9+
10+
OpenAjax.hub.subscribe("history.**", function(event_name) {
11+
var key = event_name.replace(/^history(\.*)/, '/'),
12+
foundRoute = SubscriberRouter.recognize(key)
13+
if (!foundRoute) {
14+
return;
15+
}
16+
17+
var callbacks = RouteToCallback[foundRoute.destination];
18+
if (callbacks) {
19+
for (var i = 0; i < callbacks.length; i++) {
20+
callbacks[i]();
21+
}
22+
}
23+
});
24+
25+
$.Controller.processors.route = function( el, event, selector, cb, controller ) {
26+
// Add the selector to be watched
27+
SubscriberRouter.add(selector).to(selector);
28+
29+
// Create an array of callbacks for this selector
30+
if (typeof RouteToCallback[selector] === "undefined") {
31+
RouteToCallback[selector] = [];
32+
}
33+
34+
// Add callback to this array
35+
RouteToCallback[selector].push(cb);
36+
37+
return function() {
38+
//console.debug("route unsubscribe", arguments);
39+
}
40+
};
41+
})
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module("Router Processor Controller");
2+
3+
var didRun = false;
4+
5+
$.Controller.extend("TestProcessorsController",
6+
{
7+
onDocument: true
8+
},
9+
{
10+
"/test/url route": function() {
11+
didRun = true;
12+
}
13+
});
14+
15+
asyncTest("Works", function() {
16+
expect(1)
17+
stop();
18+
location.hash = "/test/url";
19+
setTimeout(function() {
20+
start();
21+
ok(didRun, "Route worked");
22+
location.hash = "/";
23+
}, 50);
24+
});

subscribe/test/qunit/qunit.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
steal
2+
.plugins("jquery/controller") //load your app
3+
.plugins("ss/router/subscribe") //load your app
4+
.plugins("funcunit/qunit") //load qunit
5+
.then("processor_test")

0 commit comments

Comments
 (0)