Index ¦ Archives  ¦ Atom  ¦ RSS

Enyo - Improved Mixins [Updated]

I've sent in a patch for slightly enhanced mixin support for Enyo, but since I have another post depending on that support I'll post the patch here until it gets included.

This patch mainly provides the ability to have mixins that handle events and allows them to join with kinds that also have events, though it does so by supporting more than just events but any object and now any function (not array ~~or function~~) they both share.

Update 10/20/11: This can now handle functions in the mixin properly since before it would cause infinite recursion with improper inherited handling. Sorry, Enyo core team, you made a mistake that was masked by another mistake, I fixed one and the other popped up.

Also, the patch below doesn't apply well (tabs vs spaces) so here's a link to a patch that I've tested against the latest SDK (3.0.4).

--- ../palm_sdk/opt/PalmSDK/0.1/share/framework/enyo/1.0/framework/source/kernel/lang.js        2011-05-09 13:39:09.000000000 -0700
+++ ../framework/refcode/webos-framework/enyo/1.0/framework/source/kernel/lang.js       2011-09-29 22:30:21.000000000 -0700
@@ -61,6 +62,10 @@
        enyo.isString = function(it) {
                return (typeof it == "string" || it instanceof String);
        };
+
+    enyo.isObject = function(it) {
+        return (typeof it == "object" || it instanceof Object);
+    };

        //* Returns true if _it_ is a function.
        enyo.isFunction = function(it) {
--- ../palm_sdk/opt/PalmSDK/0.1/share/framework/enyo/1.0/framework/source/kernel/Oop.js 2011-09-29 22:12:04.000000000 -0700
+++ ../framework/refcode/webos-framework/enyo/1.0/framework/source/kernel/Oop.js        2011-10-01 05:03:40.000000000 -0700
@@ -185,32 +185,6 @@
        return args.callee._inherited.apply(this, newArgs || args);
};

-// 'statics' feature
-enyo.kind.features.push(function(ctor, props) {
-       // install common statics
-       enyo.mixin(ctor, enyo.kind.statics);
-       // move props statics to constructor
-       if (props.statics) {
-               enyo.mixin(ctor, props.statics);
-               delete ctor.prototype.statics;
-       }
-       // allow superclass customization
-       var base = ctor.prototype.base;
-       while (base) {
-               base.subclass(ctor, props);
-               base = base.prototype.base;
-       }
-});
-
-enyo.kind.statics = {
-       subclass: function(ctor, props) {
-               //console.log("subclassing [" + ctor.prototype.kind + "] from [", this.prototype.kind + "]");
-       },
-       extend: function(props) {
-               enyo.mixin(this.prototype, props);
-       }
-};
-
// 'mixins' feature
enyo.kind.features.push(function(ctor, props) {
        if (props.mixins) {
@@ -218,13 +192,31 @@
                 for (var i=0, m; (m=props.mixins[i]); i++) {
                         var mp = m;
                         for (var n in mp) {
+                                var v = mp[n];
                                 if (mp.hasOwnProperty(n) && !props.hasOwnProperty(n)) {
-                                        var v = mp[n];
+                                        // just add it on
                                         if (enyo.isFunction(v) && (v.toString().indexOf("inherited") >= 0)) {
                                                 cp[n] = enyo.kind._wrapFn(v, cp, n);
                                         } else {
                                                 cp[n] = v;
                                         }
+                                } else if (mp.hasOwnProperty(n) && props.hasOwnProperty(n)) {
+                                        // both have it, so try pushing it in
+                                        var w = cp[n];
+                                        if (enyo.isObject(v) && enyo.isObject(w)) {
+                                                props[n] = enyo.mixin(props[n], v);
+                                        } else if (enyo.isFunction(v)) {
+                                                if (v.toString().indexOf("inherited") >= 0) {
+                                                        cp[n] = enyo.kind._wrapFn(v, cp, n);
+                                                } else {
+                                                        // assume we want to call the original function,
+                                                        // then the mixin function
+                                                        cp[n] = function(){
+                                                                v.apply(this,arguments);
+                                                                w.apply(this,arguments);
+                                                        };
+                                                }
+                                        }
                                 }
                         }
                 }
@@ -233,6 +225,7 @@

 enyo.kind._wrapFn = function(fn, proto, name) {
         var inh = function(args) {
+                this.inherited = enyo.kind.inherited;
                 return proto.base.prototype[name].apply(this, args);
         };
         return function() {
@@ -242,6 +235,32 @@
        };
};

+
+// 'statics' feature
+enyo.kind.features.push(function(ctor, props) {
+       // install common statics
+       enyo.mixin(ctor, enyo.kind.statics);
+       // move props statics to constructor
+       if (props.statics) {
+               enyo.mixin(ctor, props.statics);
+               delete ctor.prototype.statics;
+       }
+       // allow superclass customization
+       var base = ctor.prototype.base;
+       while (base) {
+               base.subclass(ctor, props);
+               base = base.prototype.base;
+       }
+});
+
+enyo.kind.statics = {
+       subclass: function(ctor, props) {
+               //console.log("subclassing [" + ctor.prototype.kind + "] from [", this.prototype.kind + "]");
+       },
+       extend: function(props) {
+               enyo.mixin(this.prototype, props);
+       }
+};
enyo._kindCtors = {};

enyo.constructorForKind = function(inKind) {

© Fahrzin Hemmati. Built using Pelican. Theme by Giulio Fidente on github.