我想向 airbrake.io 发送由于渲染木偶视图而出现的空气制动,但我不想在视图的所有方法中都使用 try catch。有更好的方法吗?
当前实施:
try {
...
} catch (e) {
Airbrake.push(error);
}
我想向 airbrake.io 发送由于渲染木偶视图而出现的空气制动,但我不想在视图的所有方法中都使用 try catch。有更好的方法吗?
当前实施:
try {
...
} catch (e) {
Airbrake.push(error);
}
你应该使用与此类似的mixin,
var asAirBreakView = function () {
//note, this function assumes it's being called using 'apply' or 'call'
//so context could be set to view's prototype.
//store original render function
var originalRender = this.render
//replace the render function with the wrapped render function
this.render = function () {
try {
//call original render function with arguments passed in
return originalOnRender.apply(this, arguments);
} catch (e) {
Airbrake.push(error);
throw e;
}
};
};
var view = Marionette.ItemView.extend({
//define your view here
});
//Apply the mixin to prototype of your view
view = asAirBreakView.apply(view.prototype);
我真的很喜欢如何在 javascript 中为函数和类添加行为。这是您在 C# 或 java 等经典继承语言中无法获得的东西。