From 3c6c8fe1de117ec69a146ea18aedac5d8dc30691 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 5 Dec 2013 12:11:43 +0100 Subject: [PATCH] refactoring --- src/smartxml/smartxml.js | 17 +++-------------- src/smartxml/transformations.js | 17 ++++++++++++++--- src/wlxml/wlxml.js | 19 ++++--------------- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 130d5c9..202d9e6 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -794,17 +794,6 @@ $.extend(Document.prototype, Backbone.Events, { var doc = this, existingPropertyNames = _.values(this); - var getTrans = function(desc, methodName) { - if(typeof desc === 'function') { - desc = {impl: desc}; - } - if(!desc.impl) { - throw new Error('Got transformation description without implementation.') - } - desc.name = desc.name || methodName; - return desc; - }; - ['document', 'documentNode'].forEach(function(dstName) { var dstExtension = extension[dstName]; if(dstExtension) { @@ -821,11 +810,11 @@ $.extend(Document.prototype, Backbone.Events, { if(dstExtension.transformations) { _.pairs(dstExtension.transformations).forEach(function(pair) { - var transformation = getTrans(pair[1], pair[0]), + var name = pair[0], + desc = pair[1], operation; operation = {document: 'registerTransformation', documentNode: 'registerNodeTransformation'}[dstName]; - doc[operation](transformations.createContextTransformation(transformation)); - + doc[operation](transformations.createContextTransformation(desc, name)); }); } } diff --git a/src/smartxml/transformations.js b/src/smartxml/transformations.js index 6d72049..772cf75 100644 --- a/src/smartxml/transformations.js +++ b/src/smartxml/transformations.js @@ -5,8 +5,19 @@ define(function(require) { var _ = require('libs/underscore'), toret = {}; +var getTransDesc = function(desc, name) { + if(typeof desc === 'function') { + desc = {impl: desc}; + } + if(!desc.impl) { + throw new Error('Got transformation description without implementation.') + } + desc.name = desc.name || name; + return desc; +}; -toret.createGenericTransformation = function(desc) { +toret.createGenericTransformation = function(desc, name) { + desc = getTransDesc(desc, name); var GenericTransformation = function(document, args) { this.args = args || {}; @@ -64,9 +75,9 @@ toret.createGenericTransformation = function(desc) { // var t = T(doc, {a:1,b:2,c3:3}); -toret.createContextTransformation = function(desc) { +toret.createContextTransformation = function(desc, name) { // mozna sie pozbyc przez przeniesienie object/context na koniec argumentow konstruktora generic transformation - var GenericTransformation = toret.createGenericTransformation(desc); + var GenericTransformation = toret.createGenericTransformation(desc, name); var ContextTransformation = function(document, object, args) { GenericTransformation.call(this, document, args); diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index b560982..13522ba 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -268,19 +268,7 @@ $.extend(WLXMLDocument.prototype, { registerExtension: function(extension) { //debugger; smartxml.Document.prototype.registerExtension.call(this, extension); - var doc = this, - existingPropertyNames = _.values(this); - - var getTrans = function(desc, methodName) { - if(typeof desc === 'function') { - desc = {impl: desc}; - } - if(!desc.impl) { - throw new Error('Got transformation description without implementation.') - } - desc.name = desc.name || methodName; - return desc; - }; + var doc = this; _.pairs(extension.wlxmlClass).forEach(function(pair) { var className = pair[0], @@ -293,8 +281,9 @@ $.extend(WLXMLDocument.prototype, { }); _.pairs(classExtension.transformations || {}).forEach(function(pair) { - var transformation = getTrans(pair[1], pair[0]); - doc.registerClassTransformation(transformations.createContextTransformation(transformation), className); + var name = pair[0], + desc = pair[1]; + doc.registerClassTransformation(transformations.createContextTransformation(desc, name), className); }); }); -- 2.20.1