Ben Vinegar - 5 Tips For Better Javascript Applications

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Ben Vinegar - 5 Tips For Better Javascript Applications - Presentation Transcript

    1. 5 Tips for Better Javascript Applications Ben Vinegar, FreshBooks
    2. Forewarning • Most examples in jQuery • But applicable to every other framework • 5 Tips ... 5 Minutes • Brief topic introductions • Apologies for speaking fast • Wish me luck!
    3. 1. Javascript Templates
    4. Dynamic Content $('table').append(' <tr>' <td>' + first_name + '</td>' <td>' + last_name + '</td>' </tr>' ');
    5. Another Approach jsontemplate.Template(' <tr> <td>{first_name}</td> <td>{last_name}</td> </tr> ').expand({ first_name: 'Lindsay', last_name: 'Funke' });
    6. Embedded Template • <table> <tr> <th>First</th> <th>Last</th> </tr> <script type="text/html" id="row-template"> <tr> <td>{first_name}</td> <td>{last_name}</td> </tr> </script> </table> jsontemplate.Template($('#row-template').html()).expand(templateArgs);
    7. 2. Classical Inheritance (think Java)
    8. Base Class var Dialog = Class.extend({ constructor: function() { // common setup }, open: function() { /* open dialog */ }, close: function() { /* close dialog */ } });
    9. Concrete Class var EditTaxesDialog = Dialog.extend({ constructor: function() { this.super(); // setup specific to Edit Taxes }, }); var editTaxesInstance = new FancyWidget();
    10. Libraries • Standalone solutions • inheritence.js, John Resig • base.js, Dean Edwards • Framework solutions • prototype.js
    11. 3. debugger keyword
    12. 4. Custom Events Like click, change, focus, blur ... but you name them
    13. The Use Case
    14. Traditionally $('input').click(function() { disableSubmitButtons(); doSomeAjax(function() { success: function() { closeWindow(); }, error: function() { enableSubmitButtons(); } }); });
    15. Declare Events $('input').click(function() { $(document).trigger('dialog:busy'); doSomeAjax(function() { success: function() { $ (document).trigger('dialog:closed'); }, error: function() { $ (document).trigger('dialog:idle'); } }); });
    16. Capture Events • $(document).bind('dialog:busy', function() { // disable buttons }); $(document).bind('dialog:idle', function() { // enable buttons });
    17. Why Bother? • It’s gonna hurt me • One sender, many receivers • Decoupled • Sender doesn’t care who listens • Exposes hooks, plugin-able
    18. 5. Hash Marks (#)
    19. Benefits • Bookmarkable • Preserves back button • Single page apps (GMail) • Easier than it sounds
    20. Url Routing *Magic url-handling library $(document).bind('hash:load', function(hash) { // load state identified by hash // like, say, opening a particular dialog });

    + Refresh EventsRefresh Events, 2 months ago

    custom

    289 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 289
      • 289 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 3
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags