<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Werx Limited &#187; code</title> <atom:link href="http://werxltd.com/wp/tag/code/feed/" rel="self" type="application/rss+xml" /><link>http://werxltd.com/wp</link> <description>We make IT work.</description> <lastBuildDate>Mon, 07 May 2012 18:40:10 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>Primitive OOP observer pattern with ExtJS</title><link>http://werxltd.com/wp/2009/08/13/primitive-oop-with-extjs/</link> <comments>http://werxltd.com/wp/2009/08/13/primitive-oop-with-extjs/#comments</comments> <pubDate>Thu, 13 Aug 2009 16:11:45 +0000</pubDate> <dc:creator>wes</dc:creator> <category><![CDATA[software development]]></category> <category><![CDATA[code]]></category> <category><![CDATA[code quality]]></category> <category><![CDATA[coding practices]]></category> <category><![CDATA[extjs]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[software design patterns]]></category><guid
isPermaLink="false">http://werxltd.com/wp/?p=170</guid> <description><![CDATA[Recently I embarked on a quest to design a framework which would help us keep track of large and complex javascript projects in easily identifiable and extendable patterns. After some thought and deliberation, we eventually settled on the observer pattern using Ext&#8217;s Ext.util.Observable object as the base object to use in responding to event broadcasts [...]]]></description> <content:encoded><![CDATA[<p>Recently I embarked on a quest to design a framework which would help us keep track of large and complex javascript projects in easily identifiable and extendable patterns. After some thought and deliberation, we eventually settled on the <a
href="http://en.wikipedia.org/wiki/Observer_pattern">observer pattern</a> using Ext&#8217;s <a
href="http://extjs.com/deploy/dev/docs/?class=Ext.util.Observable">Ext.util.Observable</a> object as the base object to use in responding to event broadcasts and the <a
href="http://extjs.com/forum/showthread.php?t=37422">Ext.ux.BroadcastEvents</a> plugin for managing global event broadcasts.</p><p>Here is the code I came up with for common component elements:</p><pre class="brush:javascript">
ComponentWrapperBase = Ext.extend(Ext.util.Observable, {
	delayBuild: false,

	eventObj: {
			"buildwidget": true,
			"showwidget": true,
			"destroy": true,
			"refresh": true
		},

	globalEvents: [],

	widget: null,
	widgetCfg: null,

	build: function() {
		if (this.widgetCfg &#038;&#038; !this.widget) {
			//console.log("buildwidget", this);
			this.widget = Ext.ComponentMgr.create(this.widgetCfg);
			this.widget.wrapper = this;
			this.fireBuildEvent();
		}
	},

	fireBuildEvent: function() {
		this.fireEvent("buildwidget", this, this.widget);
	},

	showWidget: function(locationId) {
		if (this.widget) {
			if (this.widget.xtype == "window") {
					if (locationId != null) {
						this.widget.show(locationId);
					} else {
						this.widget.show();
					}
			}
			else {

				if (locationId != null) {
					this.widget.render(locationId);
				}
				else {
					this.widget.render();
				}
			}
			this.fireEvent("showwidget", this);
		}
	},

	constructor: function(config){
		if (config) {
			if (config.widgetCfg) {
				this.widgetCfg = config.widgetCfg;
			}

			if (config.delayBuild) {
				this.delayBuild = config.delayBuild;
			}

			// Copy configured listeners into *this* object so that the base class's
			// constructor will add them.
			if (config.listeners) {
				Ext.apply(this.listeners,config.listeners);
			}

			if(config.events &#038;&#038; Ext.isArray(config.events)) {
				for (var i=0; i<config.events.length; i++) {
					this.eventObj[config.events[i]] = true;
				};
			}

			if (config.globalEvents &#038;&#038; Ext.isArray(config.globalEvents)) {
				this.globalEvents = config.globalEvents;
			}
		}

		for (var i=0; i
<this.globalEvents.length; i++) {
			Ext.ux.event.Broadcast.subscribe(this.globalEvents[i], this.listeners[this.globalEvents[i]], this);
			this.eventObj[this.globalEvents[i]] = true;
		};

		this.addEvents(this.eventObj);

		// Call our superclass constructor to complete construction process.
		ComponentWrapperBase.superclass.constructor.call(this);

		//this.on(this.eventListeners);

		if(!this.delayBuild) {
			this.build();
		}
	}
});
</pre><p>This creates a simple widget wrapped in an Observable object. You can reach the contained widget through wrapper.widget. Using the framework above, here's how you could create a simple widget.</p><pre class="brush:javascript">
SimpleTextField = Ext.extend(ComponentWrapperBase, {
	widgetCfg: {
		xtype: 'textfield',
		fieldLabel: 'Name'
	},
	constructor: function(config){		// Constructor is not required, but I've included
							// it to demonstrate the flexibility of this framework
		if (config) {
			Ext.apply(this,config);
		}

		this.widgetCfg.value = 'John Smith';

		SimpleTextField.superclass.constructor.call(this);
	}
});

var simpleTextField = new SimpleTextField();
// Actual component is available at simpleTextField.widget, useful for adding to other containers
</pre><p>That's it! If you have any questions or comments, feel free to post them below.</p><div
class="betterrelated none"><p>No related content found.</p></div><p><a
class="a2a_button_facebook_like addtoany_special_service" data-href="http://werxltd.com/wp/2009/08/13/primitive-oop-with-extjs/"></a><a
class="a2a_button_twitter_tweet addtoany_special_service" data-count="none" data-url="http://werxltd.com/wp/2009/08/13/primitive-oop-with-extjs/" data-text="Primitive OOP observer pattern with ExtJS"></a><a
class="a2a_button_google_plusone addtoany_special_service" data-annotation="none" data-href="http://werxltd.com/wp/2009/08/13/primitive-oop-with-extjs/"></a><a
class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F08%2F13%2Fprimitive-oop-with-extjs%2F&amp;linkname=Primitive%20OOP%20observer%20pattern%20with%20ExtJS" title="LinkedIn" rel="nofollow" target="_blank"><img
src="http://werxltd.com/wp/wp-content/plugins/add-to-any/icons/linkedin.png?9d7bd4" width="16" height="16" alt="LinkedIn"/></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F08%2F13%2Fprimitive-oop-with-extjs%2F&amp;title=Primitive%20OOP%20observer%20pattern%20with%20ExtJS" id="wpa2a_2">Share/Save</a></p>]]></content:encoded> <wfw:commentRss>http://werxltd.com/wp/2009/08/13/primitive-oop-with-extjs/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using apc
Page Caching using apc
Database Caching 4/9 queries in 0.002 seconds using apc
Object Caching 332/337 objects using apc

Served from: werxltd.com @ 2012-05-21 18:08:24 -->
