Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Monday, January 16, 2012

Dipping a Toe in the Backbone Pool.

Over the last couple months my endeavors in the Web application world, both personal and professional, would have one common denominator - a lot of moving client side parts (code). With this, I knew I wasn't the first or last person to encounter the same code maintability/readability/organization obstacle so I ventured out on to see what was available and also relatively painless to ramp up on..... enter Document Cloud's Backbone.js framework. Backbone is a javascript library (built on top of Underscore.js and jQuery) meant to give some MVC structure to the otherwise Wild Wild West world of client side development. In skimming their documentation I could grasp immediately where the authors were going with it and how intuitive it was. That's not to say there isn't a learning curve but it's one of those that is well worth getting over, in my opinion.

Like anything, once you get a firm grasp of the overall framework the advanced operations kind of fall into place. There are useful resources out there written by developers in the community on advanced usage. That said, there were some pain points I encountered that made bridging the gap from rookie to novice a little challenging. With that in mind I set out to write up a sample app that would help me get over the hump. So for this entry I will dissect the included example and highlight the important stuff.



The apps purpose is simple - load a collection of items to an unordered list at run time and allow the users to add items of their own dynamically. All wired with click events to be toggled with a gray background to indicate selected and white background to indicate not selected.

NOTE: I suggest touring through the Fiddle a bit and understanding how things before continuing below. Loading it in another tab to flip back and forth to while continuing might also be beneficial.

Models
Model extensions are essentially representations of our data that we will be retrieving from our data tier and working with on the presentation layer. For this example we only have one model:

Item model - the Item model only has 2 properties (name and ordinal) and were are configuring their respective defaults. These will map seamlessly with a JSON data set, which we will see later.

Collections
Collections are, simply put, collections of Models.

ItemList - A basic instance of a collection that will contain Item models.


Views
Views are really what organizes and drives your interface; this is where you start modularizing everything. How you do this is entirely up to you, basically any element (be it the overall containing or any element somewhere in a list) on a page can be represented by a View. Once you work with Backbone a bit, you get a better feel for how to modularize your View logic.

ItemEntryView - This view will handle the user interactions with the textbox and "Add" button at the top of the page.



Notables:

el: Those familiar with jQuery the $(‘.itemEntry’) will look familiar. Here I am specifying the selector for the element (hence, el) on the page that an instance of ItemEntryView will control.

ItemView - This view will control any "Item" element that is added to the ItemsView.



Notables:


  • className: Kind of self explanatory; this is the CSS class that will be added to the element that contains the views contents. In this case it would be "item".
  • tagName: Since this view is dynamically generated, you may want to specify what kind of element will contain the views contents. In this case, I have an li element specified so these views will be output as ‹li>. *content here* ‹/li>.
  • events: The events array is a shorthand way to let you wire up events for the view. I only have one event setup for when the view is clicked it will call the "toggle" function. Simple as that and Backbone takes care of the rest.

When new views are added or updated after the initial page load, the events are attached to them (think jQuery live/delegate).

ItemsView - The view that will manage all of the ItemView instances added to the .items container.


Notables:

  • collection: Here we are essentially specifying a collection that the ItemsView will “listen” to and give context to the view when collection is referenced in other methods.
  • initialize: The constructor of a View that is called when a ItemsView is invoked. Just specifying the collection property will give you context, in order to stay in sync with the collection we bind any resets and adds to the collection with callbacks to the render and add methods, respectively. Simply put, any time a reset/add is called on the collection (outside of this view) the render/add method for this view will be called; that way the UI stays in sync with the data. This is where the meat and potatoes of Backbone really start to show themselves.
AppView - This is the page level controller that manages/sets up all of the views within the page.


Conclusion

Still some confusion? Here's a visual:

Hopefully this mess all offers another helpful look at this framework and helps someone out looking to learn up on it. There is a lot of useful functionality in this library so I am still learning the ins and outs and working it into apps, when practical.

P.S. While Backbone definitely helps alleviate a lot of typing, applications can still produce a significant amount of code. Managing that on an enterprise level would be a whole other post.
To help get an idea on how to properly arrange things I suggest taking a look at this tutorial.

Monday, May 2, 2011

jQuery UI and you. (Part 1 of 2)

Well, I had a number of potential first post topics listed out and am deciding to go with the most topical at the moment. For my post I won't be revolutionizing the game with something cutting edge but rather discussing an interface framework I've grown more and more fond of over time. If it wasn't immediately evident from the subject, that framework is jQuery UI.

To know jQuery UI, you need to know what jQuery is. At its simplest, jQuery is a Javascript API that you can bolt into any web application/site that will make your (client side programming) life infinitely easier/better. jQuery not only simplifies standard client side programming but it also makes advanced things like effects and animations much easier to achieve. With all it can do and for how lightweight it is, jQuery is quickly (if not already) replacing Flash as the go to for advanced front end effects. This is old news for most devs but there are still a great deal of people to live in the server side world only or only know what college told them to know.

The High Level
jQuery UI ( jUI ) is basically an interface widget framework that is just additional extension on top of jQuery. It offers a lot of effects and controls (buttons, calendar, sliders, drag-n-drop, etc.) that are now common place on the web in addition to some newer concepts which can be implemented easily into your front end code. They take care of the basic core functionality so, if necessary, you can focus on addressing user interactions. Additionally, all HTML elements in all of the widgets are driven by a single style sheet that is inheritance driven so it really makes changes easy to implement globally.

This sounds a lot like ASP.NET themes but is on the complete other side of the spectrum. They are roughly the same idea but jUI lets you off road a bit more and style every component of your application as one cohesive unit and not just buttons, text boxes, etc.

Keep a Programmer Programming
Personally, I like working in Photoshop and building concepts/graphics - always have. However, I am quickly learning that I am in the minority in this area as programmers tend to be more inclined to have those responsibilities be delegated to a “legit” designer and just worry about the middle tier and punch out. Sometimes you have no choice. No matter how much I enjoy it though, tasks like making icons and small graphics is tedious/time consuming and there are tons of packages available that are probably better for such a simple task. That’s where jUI comes in handy. They offer a built in icon set for all of the standard (and some advanced) actions a user might need to perform. In their typical baller style, their dev team provides all of the icons in one lightweight image and uses CSS Spriting to determine which is displayed.

What’s that all mean? Colors, icons, width, etc. are all dictated by CSS classes applied to HTML elements which makes changes a code endeavor which is more up any programmer’s alley. No time fussing with in the 15x15 pixel world in Photoshop or PSPro.

Jazz Hands
One of my favorite aspects of the framework can be summed up in one word – ThemeRoller. The jUI people have been courteous enough to provide a customization tool that lets you tweak just about every aspect of your theme (background color, gradient styles, borders, hover styling, etc.) through a web app and then have it all packaged up to be deployed into your application within minutes. That way you can make your interface as loud or muted as you want. Client wishing they would have
picked cornflower blue instead of sky blue last minute? Ain't no thang. Make the necessary changes in ThemeRoller and swap in the new CSS file. You can take it to the extent of providing them a link to the ‘Roller and let them tweak things until they are satisfied and just have them send you the link to the new theme when they are done so you can pull down the new CSS file. It’s simple as that.

.... and now its a made guy.
jQuery being bundled with Visual Studio 2010 was one of the notable additions to the IDE. Not notable because it made jQuery easier to implement
(easier to use,maybe, with Intellisense), notable because the little open source framework got a nod from the Don. Fast forward to today, things have grown to the point where MVC3 is now tightly coupled with jQueryUI framework (quite well actually) to where developers can't help but leverage it to get the job done better/faster.


Side note - The script libraries Microsoft is including seems to be the final admission that "OK, we know MSAjax _might_ have sucked."..... and lets be honest, there is an UpdatePanel or two all of us would like to take back.

Wrap Up
Before this all comes off as a fan boy material (too late!) I will admit, if you are relatively new to the UI/Client side of things there is a bit of a learning curve with jQuery and its UI extension. Additionally, out of the box, these are straight forward web components so that means you'll be wiring up your own postbacks, hombre. Which also creates another obstacle in maintaining/recovering state. So there in lies the lead in to my next installment. MVC lets you architect a lot of this in a pretty seamless manner but for the rest of us that don't have clients/companies that are willing to drop the clams on VS 2010 I'll be piecing together some basic .NET wrappers to get you going in the right direction (or at least what I think is the right direction). Alright, that's all, the gardener has gardened all day and doesn't want to mow his own lawn anymore. I hope to have Part 2 done with the next couple weeks with some other posts sprinkled in between.