moving wlxml object api transformations directly into .object properties name space
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 5 Dec 2013 15:52:38 +0000 (16:52 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 15 Dec 2013 21:32:48 +0000 (22:32 +0100)
src/wlxml/wlxml.js
src/wlxml/wlxml.test.js

index 13522ba..016f793 100644 (file)
@@ -22,23 +22,13 @@ AttributesList.prototype.keys = function() {
 };
 
 var installObject = function(instance, klass) {
 };
 
 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) {
 }
 
 var WLXMLElementNode = function(nativeNode, document) {
@@ -256,8 +246,11 @@ $.extend(WLXMLDocument.prototype, {
     },
 
     registerClassTransformation: function(Transformation, className) {
     },
 
     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) {
     },
 
     registerClassMethod: function(methodName, method, className) {
index 936093e..3d1df03 100644 (file)
@@ -281,8 +281,8 @@ describe('WLXMLDocument', function() {
             }}}};
             doc.registerExtension(extension);
             testClassNode = doc.root.contents()[1];
             }}}};
             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');
         });
 
 
         });