Skip to content

Commit

Permalink
Close #46 - Should handle exceptions in with/withOnShow
Browse files Browse the repository at this point in the history
  • Loading branch information
finnsson committed Dec 16, 2012
1 parent 7ef3f45 commit 11e80ad
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 4 deletions.
46 changes: 42 additions & 4 deletions pager.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
*/
pager.navigationFailed = ko.observable();

/**
* Called when a binding could not be applied.
*
* @var onBindingError
* @type {$.Callbacks}
* @static
*/
pager.onBindingError = $.Callbacks();

/**
*
* @param {String[]} route
Expand Down Expand Up @@ -466,6 +475,21 @@
}
};

var applyBindingsToDescendants = function(page) {
try {
ko.applyBindingsToDescendants(page.childBindingContext, page.element);
} catch(e) {
var onBindingError = page.val('onBindingError');
if(onBindingError) {
onBindingError(page. e);
}
pager.onBindingError.fire({
page: page,
error: e
});
}
};

/**
* @method pager.Page#init
*
Expand Down Expand Up @@ -521,7 +545,21 @@
$page:this,
$observableData: context
});
ko.applyBindingsToDescendants(m.childBindingContext, m.element);
applyBindingsToDescendants(m);
/*
try {
ko.applyBindingsToDescendants(m.childBindingContext, m.element);
} catch(e) {
var onBindingError = m.val('onBindingError');
if(onBindingError) {
onBindingError(m. e);
}
pager.onBindingError.fire({
page: m,
error: e
});
}
*/

context.subscribe(function() {
dataInContext(_ko.value(context));
Expand All @@ -532,7 +570,7 @@
$page:this,
$observableData: undefined
});
ko.applyBindingsToDescendants(m.childBindingContext, m.element);
applyBindingsToDescendants(m);
}
}

Expand Down Expand Up @@ -678,7 +716,7 @@
me.childBindingContext = childBindingContext;
me.augmentContext();
ko.utils.extend(childBindingContext, {$page:me});
ko.applyBindingsToDescendants(childBindingContext, me.element);
applyBindingsToDescendants(me);
}, me);
}
};
Expand Down Expand Up @@ -732,7 +770,7 @@
// TODO: call abstraction that either applies binding or loads view-model

if (!me.val('withOnShow')) {
ko.applyBindingsToDescendants(me.childBindingContext, me.element);
applyBindingsToDescendants(me);
} else if (me.val('withOnShow')) {
me.loadWithOnShow();
}
Expand Down
11 changes: 11 additions & 0 deletions test-data/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div data-bind="text: thisPropertyShouldNotExist">
</div>
<div id="errorDiv">Test</div>
</body>
</html>
65 changes: 65 additions & 0 deletions test/should_trigger_onbindingerror.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html>
<head>
<title>Should trigger onBindingError</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>

<div data-bind="page: {id: 'start', source: '../test-data/error.html', onBindingError: bindingError}">

</div>

<script type="text/javascript">

var globalIsTriggered = false;

var localIsTriggered = false;


var viewModel = {
bindingError:function () {
localIsTriggered = true;
}
};

pager.extendWithPage(viewModel);
pager.onBindingError.add(function () {
globalIsTriggered = true;
});
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 trigger onBindingError", function () {

until(function () {
return $('#errorDiv').is(':visible');
}, function () {
assert.equal(globalIsTriggered, true, "global error should be triggered");
assert.equal(localIsTriggered, true, "local error should be triggered");
start();
});


});
</script>


</body>
</html>

0 comments on commit 11e80ad

Please sign in to comment.