Created
March 1, 2014 15:17
-
-
Save quidmonkey/9291317 to your computer and use it in GitHub Desktop.
Try/Catch Decorator Wrapper for Controller and Adapter/Interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // controller proxy | |
| for (var key in this.controller) { | |
| if (this.controller.hasOwnProperty(key) && typeof this.controller[key] === 'function') { | |
| this.proxyController(model.controller[key], key); | |
| } | |
| } | |
| MvcAdapter.prototype.proxyController = function (method, key) { | |
| this.controller[key] = function () { | |
| try { | |
| method.apply(null, arguments); | |
| } catch (e) { | |
| console.error('Ruh roh, NAF fail on method: ' + name); | |
| console.error(e); | |
| } | |
| } | |
| }; | |
| // adapter proxy | |
| for (var key in this) { | |
| if (this.hasOwnProperty(key) && this[key] === 'function') { | |
| this.proxyAdapter(this[key], key); | |
| } | |
| } | |
| MvcAdapter.prototype.proxyAdapter = function (method, name) { | |
| this[name] = function () { | |
| var args = Array.prototype.slice.call(arguments); | |
| for (var i = 0; i < args.length; i++) { | |
| if (typeof args[i] === 'function') { | |
| this.proxyCallback(args[i], args, i); | |
| }; | |
| } | |
| } | |
| method.apply(null, args); | |
| } | |
| }; | |
| MvcAdapter.prototype.proxyCallback = function (method, args, index) { | |
| args[index] = function () { | |
| try { | |
| method.apply(null, arguments); | |
| } catch (e) { | |
| console.error('Ruh roh. UI Adapter fail'); | |
| console.error(e); | |
| } | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment