通过Ember.onerror处理typeError [英] Handling typeError through Ember.onerror

查看:83
本文介绍了通过Ember.onerror处理typeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够处理我的Ember应用程序中的JavaScript错误,并显示在我的应用程序模板中定义的一般模态。我可以通过为Ember.onerror定义一个函数来处理错误,但是无法找到一种方法来触发针对某些错误类型(例如TypeError)的应用程序的事件或操作。



以下是我如何接近定义 Ember.onerror

  App.report_errors =(error) - > 
console.logerror,错误
#想要使用类似下面的行
#在应用程序路由上调用一个动作
@sendshowError

#记录到api

Em.onerror = App.report_errors

这是一个完整的例子,说明了我要完成的任务: http:/ /jsfiddle.net/mandrakus/c8E3x/1/



谢谢!

解决方案

此解决方案(由Alex Speller提供)添加了一个errorReporter对象,该对象在初始化期间被注入,具有访问路由器的能力,因此路由器操作。

  App.initializer 
name:'errorReporter'
initialize:(container) - >
container.injection'reporter:error','router','router:main'
container.injection'route','errorReporter','reporter:error'

记者= container.lookup'记者:错误'
Em.onerror =(错误) - >
reporter.report错误

App.ErrorReporter = Em.Object.extend
报告:(错误) - >
console.logerror,错误
#想要能够使用像下面这样的行
#在应用程序路由上调用一个动作
@ router.send showError

App.ApplicationRoute = Ember.Route.extend
actions:
error:(error) - > @ errorReporter.report错误
showError: - >
console.log显示错误
#最终应用程序生成模态或其他通知
alert通用错误消息


I would like to be able to be able to handle javascript errors in my Ember application and display a generic modal defined in my application's templates. I'm able to process errors by defining a function for Ember.onerror, but haven't been able to find a way to trigger an event or action against my application for certain error types, for instance a TypeError.

Below is a sample of how I've approached defining Ember.onerror

App.report_errors = (error) ->
  console.log "error", error
  # Would like to be able to use something like the below line
  # to call an action on the application route
  @send "showError"

  # Log to api 

Em.onerror = App.report_errors

Here is a full example fiddle illustrating what I would like to accomplish: http://jsfiddle.net/mandrakus/c8E3x/1/

Thanks!

解决方案

This solution (courtesy Alex Speller) adds an errorReporter object which is injected during initialization with the ability to access the router and therefore router actions.

App.initializer
  name: 'errorReporter'
  initialize: (container) ->
    container.injection 'reporter:error', 'router', 'router:main'
    container.injection 'route', 'errorReporter', 'reporter:error'

    reporter = container.lookup 'reporter:error'
    Em.onerror = (error) ->
      reporter.report error

App.ErrorReporter = Em.Object.extend
  report: (error) ->
      console.log "error", error
      #Would like to be able to use something like the below line
      #to call an action on the application route
      @router.send "showError" 

App.ApplicationRoute = Ember.Route.extend
  actions: 
    error: (error) -> @errorReporter.report error
    showError: ->
      console.log "displaying error"
      #the final application generate a modal or other notification
      alert "Generic Error Message"

这篇关于通过Ember.onerror处理typeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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