我想将由于绘制牵线木偶视图而产生的空气制动器发送到airbrake.io 但我不想把try catch放在视图的所有方法中。有没有更好的方法呢?
目前的实施:
try {
...
} catch (e) {
Airbrake.push(error);
}
答案 0 :(得分:1)
你应该使用类似的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等经典继承语言中无法获得的东西。