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

查看:96
本文介绍了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.

我怎么能正确捕捉错误?

How can I catch the error properly?

谢谢!

推荐答案

我不是确定我理解你的确切目标,但是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.

如果你的问题只是未定义的变量,那么一招你可以使用is绑定 $ 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();

这会捕获错误,记录错误并继续。当然,具有这种坏绑定的元素不会受到约束。如果有一些已知的方法要处理它,那么您可以在捕获错误后添加该逻辑。也许你想检查那个元素(node)和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天全站免登陆