KnockoutJS 捕获错误绑定 [英] KnockoutJS catch errors Binding

查看:18
本文介绍了KnockoutJS 捕获错误绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕捉一个错误,纠正它并继续执行程序.http://jsfiddle.net/Gthv9/12/

I want to catch an error, correct it and continue executing the program. http://jsfiddle.net/Gthv9/12/

但是,我做不到!

如果您点击:重新检查模型 1"、重新检查模型 3" - 没问题.

If you click on: "re Check On Model1", "re Check On Model3" - it's ok.

如果您点击:重新检查模型 1"、重新检查模型 2"、重新检查模型 3" - 出现错误.

If you click on: "re Check On Model1", "re Check On Model2", "re Check On Model3" - there's an error.

Uncaught Error: Unable to parse bindings.
Message: ReferenceError: name3 is not defined;
Bindings value: text: name3 

为什么?

我将问题代码封装在 try-catch 块中 (viewModel.recheckData2()),但是,应用程序在单击 viewModel.recheckData3() 时崩溃!

I wrapped the problem code in a try-catch block (viewModel.recheckData2() ), but, the application crashes on clicking viewModel.recheckData3() !

我知道knockoutJS 存储错误状态(new model2()),但不知道我应该做什么.

I know that knockoutJS stores the error state (new model2()), but don't know what I should do.

如何正确捕捉错误?

谢谢!

推荐答案

我不确定我是否理解您的确切目标,但是当遇到此类问题时,Knockout 将停止绑定.

I am not sure that I understand your exact goal, but Knockout will stop binding when it runs into this type of an issue.

如果您的问题只是未定义的变量,那么您可以使用的一种技巧是绑定$data.name3 而不仅仅是name3.从有效对象访问未定义的属性不会导致错误.

If your issue is simply undefined variables, then one trick that you can use is to bind against $data.name3 rather than just name3. Accessing a undefined property off of a valid object does not cause an error.

如果你真的想要更强大的东西,那么你可以考虑使用 自定义绑定提供程序.

If you really want something more robust, then you could consider using a custom binding provider.

例如,您可以为真正的绑定提供程序编写一个快速包装器,例如:

For example, you could write a quick wrapper to the real binding provider like:

var ErrorHandlingBindingProvider = function() {
    var original = new ko.bindingProvider(); 

    //determine if an element has any bindings
    this.nodeHasBindings = original.nodeHasBindings;

    //return the bindings given a node and the bindingContext
    this.getBindings = function(node, bindingContext) {
        var result;
        try {
            result = original.getBindings(node, bindingContext);
        }
        catch (e) {
            if (console && console.log) {
                console.log("Error in binding: " + e.message);   
            }
        }

        return result;
    };
};

ko.bindingProvider.instance = new ErrorHandlingBindingProvider();

这将捕获错误,记录它们,然后继续.当然,具有这种坏"绑定的元素不会绑定.如果您想以某种已知的方式处理它,那么您可以在捕获错误后添加该逻辑.也许您想检查该元素(节点)和 bindingContext 以确定需要做什么.

This would catch errors, log them, and proceed. Of course the element that had this "bad" binding would not bound. If there is some known way that you want to handle it, then you could add that logic after catching the error. Maybe you want to inspect that element (node) and the bindingContext to determine what needs to be done.

示例:http://jsfiddle.net/rniemeyer/KxXqs/

更新:这是一个 3.0+ 版本,它捕获/记录绑定语法中的错误以及实际评估绑定值时的错误.http://jsfiddle.net/rniemeyer/ecbn1dmy/

UPDATE: Here is a version for 3.0+ that traps/logs errors in binding syntax as well as errors when the bound value is actually evaluated. http://jsfiddle.net/rniemeyer/ecbn1dmy/

这篇关于KnockoutJS 捕获错误绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆