1. March 2009, 13:43, by Silvan Mühlemann

MVC for Javascript Controls

I recently had to take over an unfinished project. It was an AJAX control to select multiple friends as you can find it on Facebook.

Cheap Windows 7 Key Buy Windows 7 Pro Adobe Flash Professional Cs6 Mac Buy Focalpoint 10 Cheap Buy Photoshop Elements 8 Windows Windows 7 Ultimate Product Key 94fbr Purchase Acronis Disk Director 11 Purchase Sql Server 2012 Purchase Project Portfolio Server 2007 Buy Windows Xp Installation Dragon Naturallyspeaking 10 Microsoft Office 2011 Home  Student Family Pack Mac Microsoft Visual Studio Team Foundation Server 2010 Office Visio Product Key Cheap Microsoft Windows 7 Professional Oem Purchase Oem Version Microsoft Windows 8 32 Bit Buy Windows 7 Multi License Windows 8 Cheap Upgrade Uk Windows 7 Home Premium For Sale Windows 7 64 Bit Oem Purchase Creative Suite 55 Cheap Rosettastone Irish Level 1 Mac Arobas Music Guitar Pro 6 Buy Windows 7 Military Discount Where To Buy Adobe Cs5 Master Collection Purchase Navisworks Review 2012 Autodesk Ecotect Analysis 2011 Purchase Windows 7 Professional Smileonmymac Disclabel 6 Mac Discount Windows 7 Ultimate Cheap Turbotax 2012 Premier Oem Rosettastone Swedish Level 1, 2 3 Set Mac Cheap Microsoft Office 2011 Home Business For Mac

Office 2007 Home And Student Discount

Student Discount Microsoft Access Oem Software Salary Microsoft Office Visio Premium 2010 Windows 7 Student Price Eset Smart Security 5 (64-bit) Best Buy Windows 8 Tablet Windows 7 Professional Upgrade Rosettastone English Level 1, 2, 3, 4  5 Set Mac Adobe Photoshop Cs5 Upgrade Cheap Windows 7 Cheap Computer With Windows 7 Buy Outlook Cheap Microsoft Office 2010 Professional Microsoft Office 2010 Professional Computer Software Buy Windows 7 Update Windows Xp Pro Product Key Purchase Incopy Cs55 Cheapall Cheap Microsoft Windows Web Server 2008 Oem Kigo Dvd Converter 3 Mac Microsoft Office Access 2010 (64-bit) Adobe Creative Suite 4 Design Premium Cheap Windows 7 College Students Buy Windows 8 Cd Buy Windows 7 Home Premium Purchase On Line Adobe Creative Suite 5 Production Buy Samsung Windows 8 Developer Pc Microsoft Office 2010 Professional Plus (64-bit) Solidworks 2010 Premium (32-bit) Buy Windows 7 Business License Buy Norton Ghost 150 Cheap Buy Windows 7 Key Code Buy Windows 7 Professional Product Key
Vmware Workstation 8 Windows 7 Key 2011 Windows 7 Keys Cheap Autodesk Autocad Revit Mep Suite 2009 Buy Office 2007 Home And Student Rosettastone Swedish Level 1, 2 3 Set Buy Microsoft Access 2010
Windows Downloads Vista Free Roxio Easy Media Creator Suite 9 Panic Candybar 3 Mac
Windows 7 Professional Oem Macgourmet Deluxe 3 Mac Cheap Adobe Creative Suite 55 Master Collection For Mac Oem Purchase Online Adobe Creative Suite 6 Design Premium Office 2008 Home Student Mac Buy Windows 7 Games Cheap Office 2007 Pro Nik Software Silver Efex Pro 2 Mac (64-bit) Microsoft Office 2010 Professional Price Paragon Partition Manager 9 Professional Cheap Microsoft Office 2007 Home And Student Photoshop Cs5 Mac Web Store Adobe Creative Suite 5 Web Premium Windows Vista Business With Sp2 64 Bit Adobe Creative Suite 5 Design Premium Student And Teacher Edition Office 2007 Student Discount Buy Windows 7 Serial Key Buy Windows 7 32 Bit Cheap Windows 7 64 Bit Upgrade Product Key Windows 8 Build 7989 Office Excel 2010 32 Bit Cheap Rosettastone German Autodesk Maya 2011 (64-bit) Buy Microsoft Office 2007 Home And Student Cheap Cs6 Master Collection Where To Buy Adobe Cs5 Production Premium Buy Windows 8 Upgrade Uk Cheap Illustrator Cs6 Buy Windows 7 Ultimate Buy Windows 7 Key Adobe After Effects Cs4 Mac Stellar Phoenix Ipod Recovery V2a Autodesk Autocad 2013 Mac (64-bit) Buy Windows 8 Phone Best Price Windows 7 Cheap Photoshop Cs6 Mac

Buy Windows Amazon

Can You Buy Windows 7 Photoshop Cs5 Discount Cheap Windows 7 Product Key Office 2010 Student Discount Microsoft Windows 7 Ultimate Discount Download Mac Os X Snow Leopard Buy Microsoft Office With Paypal Buy Windows 7 Jakarta Cheapest Price Windows 7 Discount Windows Xp Cheap Windows 7 Uk Purchase Windows Xp Home Buy Windows 8 Msdn Buy Photoshop Cs6 Online Purchase Dreamweaver Cs6 Cheap Os 10.8 Windows 8 64 Bit Cheapest Buy Windows 7 Pro Online Cheap Autodesk Software Oem Cheapest Price Microsoft Office 2010 Professional Plus 32 Bit Rosettastone French Level 1, 2 3 Set Mac Adobe Creative Suite 5.5 Master Collection Adobe Flash Builder 4.5 Premium Mac

friendselector_facebook

“It’s 99% complete”, I was told. Yeah, right. I counted 2 story points (without looking at the code). Soon I knew I was too optimistic: Classes calling other classes without logic. Randomly named variables. Data, creation of DOM elements, AJAX calls spread all over the place:

Architecture before refactoring (dramatized)

Architecture before refactoring (dramatized)

That’s not going to be easy. To make things worse: I am a Javascript beginner. What I did up to now was procedural Javascript code.

I felt the urge to begin everything from scratch. But I remember Joel Spolsky saying that we should resist so I decided to go for a refactoring.

As I know the MVC pattern very well from Zend Framework I tried to apply this pattern. Separation of concern! Each class has a well defined purpose. Here’s the architecture I refactored the code to:

Friend Selector object model after refactoring

  1. Model: All operation on data (i.e. the friends to be selected) is in the classes “ModelFriendListItems” (which aggregates “ModelFriendListItem”). The Ajax request fetching the friend objects from the server is located ther as AJAX is about data.
  2. View: The three views for the panes “Parent container”, “All friends” and “selected friends”. DOM manipulation only in these classes.
  3. A controller in charge of instancing and controlling the model and the views. The controller handles all events. No AJAX.

Here’s the sum up of the concerns of the parts:

DOM manipulation AJAX calls Event and input handling
Model No Yes No
View Yes No No
Controller No No Yes

As every class has his well defined concern you quickly know where to look when you need to maintain or extend the control. The name of the class and files reflects the role (ViewAllFriends.js, ModelFriendsListItems.js). Logical event and data flows.

You can try it here. Here’s how the final version looks:

Screenshot of friend selector control

Well… It took much more than the 2 story points I counted in the beginning. But it feels much better having clean code! And my colleagues having to extend this control (Leo?) are surely happy for this clean up :-)

I am sure there are other ways to model a Javascript control. What is your best practice?

Filed under: Web Development

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2013 tilllate Schweiz AG - Powered by WordPress