From: Aleksander Ɓukasz Date: Thu, 5 Dec 2013 15:52:38 +0000 (+0100) Subject: moving wlxml object api transformations directly into .object properties name space X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/7a67ffc356936a4eec4243df03fab2ad1c66c9b9?ds=sidebyside moving wlxml object api transformations directly into .object properties name space --- diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index 13522ba..016f793 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -22,23 +22,13 @@ AttributesList.prototype.keys = function() { }; var installObject = function(instance, klass) { - var methods = instance.document.classMethods[klass] || instance.document.classTransformations; - if(methods) { - instance.object = Object.create(_.extend({ - transform: function(name, args) { - // TODO: refactor with DocumentElement.transform - var Transformation = instance.document.classTransformations[klass].get(name), - transformation; - if(Transformation) { - transformation = new Transformation(instance.document, instance, args); - } - return instance.document.transform(transformation); - } - }, methods)); - _.keys(methods).forEach(function(key) { - instance.object[key] = _.bind(instance.object[key], instance); - }); - } + var methods = instance.document.classMethods[klass] || {}, + transformations = instance.document.classTransformations[klass] || {}; + + instance.object = Object.create(_.extend({}, methods, transformations)); + _.keys(methods).concat(_.keys(transformations)).forEach(function(key) { + instance.object[key] = _.bind(instance.object[key], instance); + }); } var WLXMLElementNode = function(nativeNode, document) { @@ -256,8 +246,11 @@ $.extend(WLXMLDocument.prototype, { }, registerClassTransformation: function(Transformation, className) { - var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || new transformations.TransformationStorage()); - return thisClassTransformations.register(Transformation); + var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {}); + thisClassTransformations[Transformation.prototype.name] = function(args) { + var nodeInstance = this; + return nodeInstance.transform(Transformation, args); + } }, registerClassMethod: function(methodName, method, className) { diff --git a/src/wlxml/wlxml.test.js b/src/wlxml/wlxml.test.js index 936093e..3d1df03 100644 --- a/src/wlxml/wlxml.test.js +++ b/src/wlxml/wlxml.test.js @@ -281,8 +281,8 @@ describe('WLXMLDocument', function() { }}}}; doc.registerExtension(extension); testClassNode = doc.root.contents()[1]; - expect(testClassNode.object.transform('testTransformation').sameNode(testClassNode)).to.equal(true, '1'); - expect(testClassNode.object.transform('testTransformation2').sameNode(testClassNode)).to.equal(true, '1'); + expect(testClassNode.object.testTransformation().sameNode(testClassNode)).to.equal(true, '1'); + expect(testClassNode.object.testTransformation2().sameNode(testClassNode)).to.equal(true, '1'); });