The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Cleaning up an example Ext JS form

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Edward Spencer

Posts: 148
Nickname: edspencer
Registered: Aug, 2008

Edward Spencer is a Ruby/Rails developer and the creator of the ExtJS MVC framework
Cleaning up an example Ext JS form Posted: Aug 21, 2008 8:43 AM
Reply to this message Reply

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
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Edward Spencer
Latest Posts From Ed's Elite blog

Advertisement
One of my recent Ext JS forms had a section which looked like this:

items: [
new Ext.Button({
text: 'Preview Video',
iconCls: 'play',
handler: function() {
var win;

if (!win) {
win = new Ext.Window({
title: 'Preview Video',
modal: true,
height: 377,
width: 368,
items: [
new Ext.Panel({
autoLoad: '/admin/videos/' + video_id + '/preview.html'
})
],
buttons: [
{
text: 'OK',
handler: function() {
win.close();
}
}
]
});

};
win.show();

}
})
]

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 || {};

Ext.applyIf(config, {
title: 'Preview Video',
modal: true,
height: 377,
width: 368,
items: [
new Ext.Panel({
autoLoad: '/admin/videos/' + config.video_id + '/preview.html'
})
],
buttons: [
{
text: 'OK',
scope: this,
handler: function() {
this.window.close();
}
}
]
});

AdFunded.views.Video.PreviewWindow.superclass.constructor.call(this, config);

this.window = this;
};
Ext.extend(AdFunded.views.Video.PreviewWindow, Ext.Window);
Ext.reg('video_preview_window', AdFunded.views.Video.PreviewWindow);

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();
}
});

AdFunded.views.Video.PreviewButton.superclass.constructor.call(this, config);
};
Ext.extend(AdFunded.views.Video.PreviewButton, Ext.Button);
Ext.reg('video_preview_button', AdFunded.views.Video.PreviewButton);

Which leaves us with the following the the view:

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.

Read: Cleaning up an example Ext JS form

Topic: TMail: Sending email Previous Topic   Next Topic Topic: Oooh Shiny! Magpies don't know what's just enough!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use