Permalink
Browse files

view controller changes based on feedback from John

  • Loading branch information...
1 parent 9503e78 commit 5673a8262247abd1ff0ddf5000738cfa3de89b4f Ryan Campbell committed Mar 2, 2012
Showing with 49 additions and 46 deletions.
  1. +3 −2 src/js/Deft/mixin/Controllable.js
  2. +46 −20 src/js/Deft/mvc/ViewController.js
  3. +0 −24 src/js/Deft/mvc/ViewControllerManager.js
@@ -8,13 +8,14 @@
Used in conjunction with {@link Deft.mvc.ViewController}.
*/
Ext.define('Deft.mixin.Controllable', {
- requires: ['Deft.mvc.ViewControllerManager'],
+ requires: ['Deft.mvc.ViewController'],
/**
@private
*/
onClassMixedIn: function(targetClass) {
targetClass.prototype.constructor = Ext.Function.createInterceptor(targetClass.prototype.constructor, function() {
- return Deft.mvc.ViewControllerManager.attach(this.controller, this);
+ var controller = Ext.create(this.controller, this);
+ this.addListener('initialize', controller.configure, controller);
});
}
});
@@ -14,35 +14,61 @@ Ext.define('Deft.mvc.ViewController', {
return this;
},
/**
- Configure the Injector.
+ Configure the ViewController.
*/
configure: function() {
Ext.Logger.log('Configuring view controller.');
+
if (Ext.isObject(this.control)) {
- Ext.Object.each(this.control, function(selector, config) {
- var components = this.view.query(selector);
- if (components.length > 0) {
- var component = components[0];
- if (Ext.isString(config)) {
- Ext.Logger.log("adding " + config + " component ref");
- this[config] = component;
- } else {
- if (Ext.isString(config.ref)) {
- Ext.Logger.log("adding " + config.ref + " component ref");
- this[config.ref] = component;
- }
- if (Ext.isObject(config.listeners)) {
- Ext.Object.each(config.listeners, function(event, handler, obj) {
- Ext.Logger.log("adding component " + component + " event " + event + " listener to " + handler );
- component.addListener(event, this[handler], this);
- }, this);
- }
- }
+ Ext.Object.each(this.control, function(key, config) {
+ var isView = key == "view";
+ var selector = Ext.isString(config) ? config : Ext.isString(config.selector) ? config.selector : "#" + key;
+ var component = isView ? this.view : this.view.query( selector )[0];
+ this[key] = component;
+ if (Ext.isObject(config)) {
+ var listeners = Ext.isObject(config.listeners) ? config.listeners : config;
+ Ext.Object.each(listeners, function(event, handler, obj) {
+ Ext.Logger.log("adding component " + component + " event " + event + " listener to " + handler );
+ component.addListener(event, this[handler], this);
+ }, this);
}
}, this);
}
+
if (Ext.isFunction(this.setup)) {
this.setup();
}
+
+ this.view.removeListener('initialize', this.configure, this);
+ this.view.addListener('beforedestroy', this.destroy, this);
+ },
+
+ /**
+ * Destroy the ViewController.
+ */
+ destroy: function(e) {
+ if (Ext.isFunction(this.tearDown)) {
+ if (this.tearDown() == false)
+ return false; // cancel view destroy
+ }
+
+ if (Ext.isObject(this.control)) {
+ Ext.Object.each(this.control, function(key, config) {
+ var component = this[key];
+ if (Ext.isObject(config)) {
+ var listeners = Ext.isObject(config.listeners) ? config.listeners : config;
+ Ext.Object.each(listeners, function(event, handler, obj) {
+ Ext.Logger.log("removing component " + component + " event " + event + " listener to " + handler );
+ component.removeListener(event, this[handler], this);
+ }, this);
+ }
+ this[key] = null;
+ }, this);
+ }
+
+ this.view.removeListener('beforedestroy', this.destroy, this);
+
+ return true;
}
+
});
@@ -1,24 +0,0 @@
-/*
- Copyright (c) 2012 [DeftJS Framework Contributors](http://deftjs.org)
- Open source under the [MIT License](http://en.wikipedia.org/wiki/MIT_License).
- */
-/**
- A lightweight MVC view controller.
-
- Used in conjunction with {@link Deft.mixin.Controllable}.
- */
-Ext.define('Deft.mvc.ViewControllerManager', {
- requires: ['Deft.mvc.ViewController'],
- singleton: true,
- constructor: function() {
- return this;
- },
- /**
- Create view controller instance and attach it to target view.
- */
- attach: function(controllerClass, targetView) {
- var controller = Ext.create(controllerClass, targetView);
- targetView.addListener('show', controller.configure, controller);
- return controller;
- }
-});

0 comments on commit 5673a82

Please sign in to comment.