Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Romain Courteaud
slapos.core
Commits
2bb1d0e5
Commit
2bb1d0e5
authored
May 31, 2012
by
Thomas Lechauve
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
New: Searching a route and call its callback
parent
147aa91b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
20 deletions
+43
-20
jquery.urlJS/src/jquery.urljs.js
jquery.urlJS/src/jquery.urljs.js
+27
-8
jquery.urlJS/test/jquery.urljs_test.js
jquery.urlJS/test/jquery.urljs_test.js
+16
-12
No files found.
jquery.urlJS/src/jquery.urljs.js
View file @
2bb1d0e5
...
...
@@ -5,12 +5,12 @@
routes
:
{
list
:
[],
current
:
null
,
add
:
function
(
route
,
level
,
callback
,
context
)
{
var
r
,
keys
,
i
;
if
(
typeof
this
.
list
[
level
]
===
'
undefined
'
)
{
this
.
list
[
level
]
=
[];
}
route
=
route
.
replace
(
/:
\w
+/g
,
'
([^
\
/]+)
'
);
r
=
{
'
route
'
:
route
,
'
level
'
:
level
,
...
...
@@ -27,28 +27,35 @@
i
=
this
.
list
[
level
].
length
;
this
.
list
[
level
][
i
]
=
r
;
},
clean
:
function
(
level
)
{
this
.
list
=
this
.
list
.
slice
(
0
,
level
);
},
cleanAll
:
function
()
{
this
.
list
=
this
.
list
.
slice
(
0
,
0
);
},
search
:
function
(
hash
)
{
var
stop
=
false
,
i
=
0
,
j
=
0
,
i
=
0
,
j
=
0
,
regex
,
result
;
console
.
log
(
hash
)
result
,
extracted
;
while
((
stop
===
false
)
&&
(
i
<
this
.
list
.
length
))
{
j
=
0
;
while
((
stop
===
false
)
&&
(
j
<
this
.
list
[
i
].
length
))
{
regex
=
new
RegExp
(
'
^
'
+
this
.
list
[
i
][
j
].
route
+
'
$
'
);
extracted
=
$
.
router
.
extractKeys
(
this
.
list
[
i
][
j
].
route
);
regex
=
new
RegExp
(
'
^
'
+
extracted
.
regex
+
'
$
'
);
if
(
regex
.
test
(
hash
.
route
))
{
result
=
regex
.
exec
(
hash
.
route
);
stop
=
true
;
console
.
log
(
result
)
result
.
shift
();
//delete hash.route;
for
(
var
k
=
0
;
k
<
result
.
length
;
k
+=
1
)
{
hash
[
extracted
.
keys
[
k
]]
=
result
[
k
];
}
this
.
current
=
this
.
list
[
i
][
j
];
this
.
clean
(
i
+
1
);
this
.
list
[
i
][
j
].
callback
(
hash
);
}
j
+=
1
;
...
...
@@ -58,6 +65,18 @@
}
},
extractKeys
:
function
(
regex
)
{
var
re_key
=
new
RegExp
(
/:
(\w
+
)
/
),
keys
=
[],
result
;
while
(
re_key
.
test
(
regex
))
{
result
=
re_key
.
exec
(
regex
);
keys
.
push
(
result
[
1
]);
regex
=
regex
.
replace
(
result
[
0
],
'
([^
\
/]+)
'
);
}
return
{
'
regex
'
:
regex
,
'
keys
'
:
keys
}
},
deserialize
:
function
(
params
)
{
var
result
=
{},
p
,
...
...
jquery.urlJS/test/jquery.urljs_test.js
View file @
2bb1d0e5
...
...
@@ -45,21 +45,25 @@ $(function () {
test
(
'
search test
'
,
function
()
{
var
url1
=
{
'
route
'
:
'
#/new/path
'
,
'
param1
'
:
'
foo1
'
,
'
param2
'
:
'
foo2
'
,
'
filter
'
:
true
},
url2
=
{
'
route
'
:
'
#/new/path/1
'
,
'
param1
'
:
'
foo1
'
},
spy1
=
sinon
.
spy
()
,
spy
2
=
sinon
.
spy
();
console
.
log
(
url1
);
$
.
router
.
routes
.
add
(
url1
.
route
,
0
,
spy1
);
$
.
router
.
routes
.
add
(
'
#/new/path/:id
'
,
1
,
spy2
);
url3
=
{
'
route
'
:
'
#/new/path/1/foo
'
,
'
param1
'
:
'
foo1
'
}
,
spy
=
sinon
.
spy
();
$
.
router
.
routes
.
add
(
'
#/new/path
'
,
0
,
spy
);
$
.
router
.
routes
.
add
(
'
#/new/path/:id
'
,
1
,
spy
);
$
.
router
.
routes
.
add
(
'
#/new/path/:id
/:other
'
,
2
,
spy
);
$
.
router
.
routes
.
search
(
url1
);
//delete url1.route;
//ok(spy1.calledOnce);
//ok(spy1.calledWith(url1));
delete
url1
.
route
;
ok
(
spy
.
calledWith
(
url1
));
//$.router.routes.search(url2);
//delete url2.route;
//ok(spy2.calledOnce);
//ok(spy2.calledWith(url2));
$
.
router
.
routes
.
search
(
url2
);
delete
url2
.
route
;
ok
(
spy
.
calledWith
(
url2
));
$
.
router
.
routes
.
search
(
url3
);
delete
url3
.
route
;
ok
(
spy
.
calledWith
(
url3
));
ok
(
spy
.
calledThrice
);
});
module
(
'
router methods tests
'
,
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment