tag attribute javascript aop

javascript - attribute - title html css



Librerías AOP Javascript (3)

¿Has visto meld.js y aop.js desde https://github.com/cujojs ?

SpringSource proporciona la funcionalidad AOP allí, además de muchas otras cosas útiles para los programadores de Javascript avanzados.

Descargo de responsabilidad : yo trabajo para SpringSource.

¿Qué biblioteca de Javascript AOP usas y cuáles son sus características clave?


Basado en la solución de dotvoid, creé mi propia versión de JS AOP para las necesidades de mis propios proyectos. Básicamente quiero minimizar los costos de instalación de aspecto, así que agregué la funcionalidad de configuración de aspecto en Function.prototype.

Function.prototype.applyBefore = function (aspect, targetFuncNames) { .... }

También necesito admitir callbacks aync, como soporte de autenticación y autorización para ciertos métodos. Por ejemplo:

var authenticateAspect = function (error, success, context, args) { logger.log(''authenticate (applyBefore async) aspect is being called''); var request = $.ajax({ url: "http://localhost/BlogWeb/api/user/authenticate", type: "GET", data: { username:''jeff'', pwd:''jeff'' }, success: function (data) { if (data) { success(); } else { error(); } }, error: error }); return request; }; Person.applyBefore(authenticateAspect, ''sendNotification''); var p1 = new Person(); p1.sendNotification();

Para implementar esto, necesito ejecutar la seguridad y continuar con el éxito o detener la ejecución en caso de falla.

var invalidAspect = new Error("Missing a valid aspect. Aspect is not a function."), invalidMethod = new Error("Missing valid method to apply aspect on."); ///Parameters: aspect - defines the methods we want call before or/and /// after each method call ob target obejct /// targetFuncNames - target function names to apply aspects ///Return: it should return a new object with all aspects setup on target object Function.prototype.applyBefore = function (aspect, targetFuncNames) { if (typeof (aspect) != ''function'') throw invalidAspect; if (typeof (targetFuncNames) != ''object'') targetFuncNames = Array(targetFuncNames); var targetObj = this; //error handling function // Copy the properties over onto the new prototype for (var i = 0, len = targetFuncNames.length; i < len; i++) { var funcName = targetFuncNames[i]; var targetFunc = targetObj.prototype[funcName]; if (!targetFunc) throw invalidMethod; targetObj.prototype[funcName] = function () { var self = this, args = arguments; var success = function() { return targetFunc.apply(self, args); }; var error = function () { logger.log(''applyBefore aspect failed to pass''); //log the error and throw new error throw new Error(''applyBefore aspect failed to pass''); }; var aspectResult = aspect.apply(null, Array.prototype.concat([error, success, self], args)); return aspectResult; }; } };

La implementación completa se puede encontrar en http://www.jeffjin.net/aop-with-javascript


Esto es lo que encontré hasta ahora:

  • la implementación de dotvoid , sintaxis limpia, agradable de usar, el artículo es una buena introducción sobre por qué / cómo usar el código dado, admite introducciones, pero tiene errores,
  • Dojo tiene lo que parece ser una buena implementación dojox en dojox , here hay una buena introducción sobre cómo usarlo,
  • hay un complemento para jQuery, jquery-aop , con una sintaxis más áspera, pasando objetos y métodos en un objeto javascript,
  • AspectJS con una sintaxis aún más áspera (necesidad de pasar el tipo de corte puntual como argumentos a un solo método)

Como dije, el código de dotvoid no funcionó. Corregí un poco y obtuve algo que parece funcionar mejor:

InvalidAspect = new Error("Missing a valid aspect. Aspect is not a function."); InvalidObject = new Error("Missing valid object or an array of valid objects."); InvalidMethod = new Error("Missing valid method to apply aspect on."); function doBefore(beforeFunc,func){ return function(){ beforeFunc.apply(this,arguments); return func.apply(this,arguments); }; } function doAfter(func, afterFunc){ return function(){ var res = func.apply(this,arguments); afterFunc.apply(this,arguments); return res; }; } Aspects = function(){}; Aspects.prototype={ _addIntroduction : function(intro, obj){ for (var m in intro.prototype) { obj.prototype[m] = intro.prototype[m]; } }, addIntroduction : function(aspect, objs){ var oType = typeof(objs); if (typeof(aspect) != ''function'') throw(InvalidAspect); if (oType == ''function''){ this._addIntroduction(aspect, objs); } else if (oType == ''object''){ for (var n = 0; n < objs.length; n++){ this._addIntroduction(aspect, objs[n]); } } else{ throw InvalidObject; } }, addBefore : function(aspect, obj, funcs){ var fType = typeof(funcs); if (typeof(aspect) != ''function'') throw(InvalidAspect); if (fType != ''object'') funcs = Array(funcs); for (var n = 0; n < funcs.length; n++){ var fName = funcs[n]; var old = obj.prototype[fName]; if (!old) throw InvalidMethod; var res = doBefore(aspect,old) obj.prototype[fName] = res; } }, addAfter : function(aspect, obj, funcs) { if (typeof(aspect) != ''function'') throw InvalidAspect; if (typeof(funcs) != ''object'') funcs = Array(funcs); for (var n = 0; n < funcs.length; n++) { var fName = funcs[n]; var old = obj.prototype[fName]; if (!old) throw InvalidMethod; var res = doAfter(old,aspect); obj.prototype[fName] = res; } }, addAround : function(aspect, obj, funcs){ if (typeof(aspect) != ''function'') throw InvalidAspect; if (typeof(funcs) != ''object'') funcs = Array(funcs); for (var n = 0; n < funcs.length; n++) { var fName = funcs[n]; var old = obj.prototype[fName]; if (!old) throw InvalidMethod; var res = aspect(old); obj.prototype[fName] = res; } return true; } }