【问题标题】:John Papa's HotTowel this.handleError is undefined...Why?John Papa 的 HotTowel this.handleError 未定义...为什么?
【发布时间】:2015-05-08 21:58:55
【问题描述】:

我使用 John Papa 的 HotTowel 模板。我有一个这样的视图模型:

define(['durandal/app', 'services/appsecurity', 'services/unitofwork', 'services/errorhandler', 'services/logger', 'services/utils'],
function (app, appsecurity, unitofwork, errorhandler, logger, utils) {

...
...

unitofwork.commit()
                .then(function () {
                    logger.logSuccess("success!", null, null, true);
                })
                .fail(function () {
                    debugger;
                    this.handleError  //UNDEFINED!!!
                });

如果我在提交过程中遇到错误,我想我应该收到一条 toast 消息或一些关于错误的通知。 我调试了代码,发现在失败函数中“this”是窗口对象,但它没有handleError函数。

这个项目模板应该如何做错误处理,我错过了什么?

【问题讨论】:

  • this 在 javascript 中表示上下文,而不是对象。我认为在您的代码中, this = window.是吗?
  • 我认为errorhandler服务应该以某种方式将自己注册到窗口,并且应该定义window.handleError。但是怎么做呢?
  • 我建议阅读以下内容:javascriptissexy.com/…。简而言之,有两种方法:将 this 捕获到变量 var self = this; (谷歌示例)或使用 Bind() 方法。
  • 请向我们展示完整的文件以及handleError 函数的定义位置

标签: knockout.js error-handling breeze durandal hottowel


【解决方案1】:

因为您没有显示一个。 试试这个

unitofwork.commit()
            .then(function () {
                logger.logSuccess("success!", null, null, true);
            })
            .fail(function () {
                var message = 'Failed Commit !!';
               logger.logError(message, error);// this will display a toast for error occured with respective moduleID name.
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多