From 11e80adf5bef1101614d92f79e4a39d50ed0f846 Mon Sep 17 00:00:00 2001 From: Oscar Finnsson Date: Sun, 16 Dec 2012 10:01:25 +0100 Subject: [PATCH] Close #46 - Should handle exceptions in with/withOnShow --- pager.js | 46 +++++++++++++++-- test-data/error.html | 11 +++++ test/should_trigger_onbindingerror.html | 65 +++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 test-data/error.html create mode 100644 test/should_trigger_onbindingerror.html diff --git a/pager.js b/pager.js index 40da222..f63729f 100644 --- a/pager.js +++ b/pager.js @@ -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 @@ -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 * @@ -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)); @@ -532,7 +570,7 @@ $page:this, $observableData: undefined }); - ko.applyBindingsToDescendants(m.childBindingContext, m.element); + applyBindingsToDescendants(m); } } @@ -678,7 +716,7 @@ me.childBindingContext = childBindingContext; me.augmentContext(); ko.utils.extend(childBindingContext, {$page:me}); - ko.applyBindingsToDescendants(childBindingContext, me.element); + applyBindingsToDescendants(me); }, me); } }; @@ -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(); } diff --git a/test-data/error.html b/test-data/error.html new file mode 100644 index 0000000..88cbf72 --- /dev/null +++ b/test-data/error.html @@ -0,0 +1,11 @@ + + + + + + +
+
+
Test
+ + \ No newline at end of file diff --git a/test/should_trigger_onbindingerror.html b/test/should_trigger_onbindingerror.html new file mode 100644 index 0000000..e25c7b5 --- /dev/null +++ b/test/should_trigger_onbindingerror.html @@ -0,0 +1,65 @@ + + + + Should trigger onBindingError + + + + + + + + + + +
+ +
+ +
+ + + + + + + + + + + \ No newline at end of file