我希望能够在我的Ember应用程序中处理javascript错误并显示在我的应用程序模板中定义的通用模式。我可以通过为Ember.onerror定义一个函数来处理错误,但是无法找到一种方法来针对某些错误类型触发针对我的应用程序的事件或操作,例如TypeError。
以下是我如何处理定义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
这是一个完整的示例小提琴,说明了我想要完成的任务:http://jsfiddle.net/mandrakus/c8E3x/1/
谢谢!
答案 0 :(得分:1)
此解决方案(Alex Speller提供)添加了一个errorReporter对象,该对象在初始化期间注入,具有访问路由器和路由器操作的能力。
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"
答案 1 :(得分:0)
这个怎么样?
App.report_errors = function (error) {
App.__container__.lookup("route:application").send("showError", error);
};
两个潜在的问题: