Skip to content
Browse files

page-href with params. Issue #89

  • Loading branch information...
1 parent 335ee98 commit b7dc876010bfe54e3c0041a7ee980d5bf7687979 @finnsson committed Feb 16, 2013
Showing with 102 additions and 39 deletions.
  1. +27 −39 pager.js
  2. +75 −0 test/should_allow_params_in_href.html
View
66 pager.js
@@ -17,10 +17,10 @@
* @param scope
* @return {Function}
*/
- var makeComputed = function(fn, scope) {
- return function() {
+ var makeComputed = function (fn, scope) {
+ return function () {
var args = arguments;
- return ko.computed(function() {
+ return ko.computed(function () {
return fn.apply(scope, args);
});
};
@@ -489,8 +489,24 @@
page = me;
} else if (p instanceof pager.Page) {
page = p;
- } else {
- page = me.find(p);
+ } else { // if string
+ if (p.substring(0, 1) === '/') {
+ return pager.page.getFullRoute()() + p.substring(1);
+ }
+ var parentsToTrim = 0;
+ while (p.substring(0, 3) === '../') {
+ parentsToTrim++;
+ p = p.slice(3);
+ }
+
+ var fullRoute = me.getFullRoute()();
+ var parentPath = fullRoute.slice(0, fullRoute.length - parentsToTrim).join('/');
+ var fullPathWithoutHash = (parentPath === '' ? '' : parentPath + '/') + p;
+ if (pager.useHTML5history) {
+ return $('base').attr('href') + fullPathWithoutHash;
+ } else {
+ return pager.Href.hash + fullPathWithoutHash;
+ }
}
var pagePath = page.getFullRoute()().join('/');
if (pager.useHTML5history) {
@@ -658,7 +674,7 @@
var urlToggle = m.val('urlToggle');
var id = m.val('id');
- if(id !== '?') {
+ if (id !== '?') {
m.getCurrentId(id);
}
@@ -818,9 +834,9 @@
return this.val('id');
};
- p.id = function() {
+ p.id = function () {
var currentId = this.getCurrentId();
- if(currentId == null || currentId === '') {
+ if (currentId == null || currentId === '') {
return this.getId();
} else {
return currentId;
@@ -1203,47 +1219,19 @@
var me = this;
var page = me.getParentPage();
- // TODO: this should use $page.path instead, but $page.find need to handle wildcards first
- /*
- me.path = ko.computed(function() {
+ me.path = ko.computed(function () {
var value = _ko.value(me.pageOrRelativePath()());
+ var x = page.path(value);
return page.path(value);
});
-
- */
- this.path = ko.computed(function () {
- var value = _ko.value(this.pageOrRelativePath()());
- if (typeof(value) === 'string') {
- if (value.substring(0, 1) === '/') {
- return pager.page.getFullRoute()() + value.substring(1);
- }
- var parentsToTrim = 0;
- while (value.substring(0, 3) === '../') {
- parentsToTrim++;
- value = value.slice(3);
- }
-
- var fullRoute = page.getFullRoute()();
- var parentPath = fullRoute.slice(0, fullRoute.length - parentsToTrim).join('/');
- return (parentPath === '' ? '' : parentPath + '/') + value;
- } else if (value.getFullRoute) {
- return value.getFullRoute()().join('/');
- }
- return "";
- }, this);
-
};
pager.Href.hash = '#';
hp.bind = function () {
- var hash = ko.computed(function () {
- return pager.Href.hash + this.path();
- }, this);
-
ko.applyBindingsToNode(this.element, {
attr:{
- 'href':hash
+ 'href':this.path
}
});
};
View
75 test/should_allow_params_in_href.html
@@ -0,0 +1,75 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Should allow params in href</title>
+ <script type="text/javascript" src="../lib/jquery-1.7.2.min.js"></script>
+ <script type="text/javascript" src="../lib/underscore-min.js"></script>
+ <script type="text/javascript" src="../lib/knockout-2.1.0.js"></script>
+ <script type="text/javascript" src="../lib/jquery.ba-hashchange.min.js"></script>
+ <script type="text/javascript" src="../pager.js"></script>
+
+ <link rel="stylesheet" href="../lib/qunit.css"/>
+</head>
+<body>
+
+<div id="qunit"></div>
+
+<a id="click" data-bind="page-href: {path: 'first', params: {nam: 'foobar', ans: 42}}">click me</a>
+
+<div data-bind="page: {id: 'first', params: ['nam','ans']}">
+ <div id="answer" data-bind="text: ans"></div>
+ <div id="name" data-bind="text: nam"></div>
+</div>
+
+<script type="text/javascript">
+
+
+ var viewModel = {
+ };
+
+ pager.extendWithPage(viewModel);
+ ko.applyBindings(viewModel);
+
+ pager.startHashChange();
+</script>
+
+<script type="text/javascript" src="../lib/qunit-until.js"></script>
+<script type="text/javascript" src="../lib/qunit.js"></script>
+
+<script type="text/javascript">
+
+ asyncTest("Should allow params in href", function () {
+
+ $(function () {
+
+
+ window.location.hash = '#';
+
+ until(function () {
+ return !$('#answer').is(':visible');
+ }, function () {
+ until(function () {
+ return $('#click').attr('href') === '#first?nam=foobar&ans=42';
+ }, function () {
+
+
+ location.hash = $('#click').attr('href');
+
+ until(function () {
+ return $('#answer').text() === '42' && $('#name').text() === 'foobar'
+ }, function () {
+ ok(true, "answer and name is set");
+ start();
+ });
+
+ });
+
+ });
+ });
+
+ });
+</script>
+
+
+</body>
+</html>

0 comments on commit b7dc876

Please sign in to comment.
Something went wrong with that request. Please try again.