This post originated from an RSS feed registered with Ruby Buzz
by Edward Spencer.
Original Post: Cleaning up an example Ext JS form
Feed Title: Ed's Elite blog
Feed URL: http://feeds2.feedburner.com/EdSpencer
Feed Description: Ruby on Rails development, Git issues and ExtJS/JavaScript
Not horrific but not nice either - let's DRY this up. It's not too pleasant to read but all it's really doing is rendering a customised Ext.Button which opens up a modal Ext.Window, in which is loaded the contents of a known url.
Ok so let's start with that Window. First, we'll make a subclass of Ext.Window:
/** * AdFunded.views.Video.PreviewWindow * @extends Ext.Window * A simple Preview window for the given video_id */ AdFunded.views.Video.PreviewWindow = function(config) { var config = config || {};
Note the namespacing employed above - within an Ext MVC framework I have been developing across several projects for the last few months, all views follow this structure. AdFunded is the name of the application. The precise structure doesn't matter here, but using a namespace for each app does.
So we've taken the Window setup out of our view now, which leaves us with:
items: [ new Ext.Button({ text: 'Preview Video', iconCls: 'play', handler: function() { var win;
if (!win) { win = new AdFunded.views.Video.PreviewWindow({video_id: id}); }; win.show();
} }) ]
Great - we've gone from 34 lines in our view to 15, and scored ourselves a reusable Window component which we can call from anywhere in the app. Nice work, but there's more to come... If we're going to use the Preview Window again, we'll probably need to use that Preview Button again too. Let's see:
/** * AdFunded.views.Video.PreviewButton * @extends Ext.Button * Displays a Preview Window for the given video_id */ AdFunded.views.Video.PreviewButton = function(config) { var config = config || {};
Ext.applyIf(config, { text: 'Preview Video', iconCls: 'play', handler: function() { var win = new AdFunded.views.Video.PreviewWindow({video_id: config.video_id}); win.show(); } });
items: [ { xtype: 'video_preview_button', video_id: id } ]
We've now gone from 34 lines to 6 (in the view at least), but the point is not about cutting out lines of code - it's creating reusable components. We've added 20 lines overall this way but we now have two extra components that we can call on at any time (with minimal lines of code), safe in the knowledge that they will provide a consistent experience each time.