Skip to content

Instantly share code, notes, and snippets.

@jacobg
Created December 3, 2012 20:18
Show Gist options
  • Save jacobg/4197694 to your computer and use it in GitHub Desktop.
Save jacobg/4197694 to your computer and use it in GitHub Desktop.
Enhancement to DeftJS ViewController class to support: (a) controls defined in each class in object class hierarchy; (b) behavior classes (similar to mixins) which are merged into ViewController object.
Ext.define('My.mvc.ViewController', {
extend: 'Deft.mvc.ViewController',
constructor: function () {
var me = this;
me.control = me.control ? Ext.clone(me.control) : {};
me.mergeBehaviors(me);
me.mergeParents();
return me.callParent(arguments);
},
mergeControls: function (Class) {
var me = this;
Ext.mergeIf(me.control, Class.control);
},
mergeBehavior: function (behaviorName) {
//
// First, get behavior class from name.
//
var me = this,
behavior = Ext.ClassManager.get(behaviorName),
prototype;
if (!behavior) {
Ext.syncRequire(behaviorName);
behavior = Ext.ClassManager.get(behaviorName);
}
prototype = behavior.prototype;
//
// Now, do merge, which involves:
// * merge controls (controller class takes precedence)
// * apply behavior injections
// * merge rest of behavior class (controller class takes precedence)
//
Ext.mergeIf(me.control, prototype.control);
if (Ext.isObject(prototype.inject)) {
Deft.Injector.inject(prototype.inject, me, false);
}
Ext.mergeIf(me, prototype);
},
mergeBehaviors: function (Class) {
var me = this,
behaviors = Class.behaviors,
behaviorName;
if (behaviors instanceof Array) {
for (i = 0,ln = behaviors.length; i < ln; i++) {
behaviorName = behaviors[i];
me.mergeBehavior(behaviorName);
}
}
else {
for (behaviorName in behaviors) {
if (behaviors.hasOwnProperty(behaviorName)) {
me.mergeBehavior(behaviorName);
}
}
}
},
mergeClass: function (Class) {
var me = this;
me.mergeControls(Class);
if (me.behaviors) {
me.mergeBehaviors(Class);
}
},
mergeParents: function () {
var me = this,
superclass = me.superclass;
while (Ext.isObject(superclass) && superclass.$className != arguments.callee.$owner.$className) {
me.mergeClass(superclass);
superclass = superclass.superclass;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment