JQuery Conference

Portland



Jquery UI vs Native

Explored the current state of native ui elements.

  • TL;DR native is not yet very customizable/widespread.
  • Interesting use of < datalist > element to provide autocomplete
  • HTML5 has new support for validation
  • jQuery UI is easier to style
  • Number Picker (ready for primetime)

Css3 Animations vs JQuery

Use jQuery animations when:

  • Performance is not an issue
  • Animation is critical to use-ability

Use css3 when:

  • Performance is critical
  • Compatibility is not critical
  • Animations are just icing on the cake

Css3 Animation Events:

transitionend (webkitTransitionEnd) (MSTransitionEnd?)
animationstart animationend animationiteration(webkitAnimationStart, webkitAnimationEnd, webkitAnimationIteration)(MSAnimationStart, MSAnimationEnd, MSAnimationIteration) http://tinyurl.com/lokazv4



Useful Links

http://prefixr.com - vendor prefix preprocessor
http://leaverou.github.io/prefixfree/ - runtime processor
http://css-tricks.com - lots of ccs tricks
http://css3please.com - css3 demos
http://daneden.me/animate/ - animate.css effects library
http://css3playground.com - some cool effects

JQuery UI TouchPunch

The plugin that almost never was

JS FACTS (that allow punching):

  1. Very little in JavaScript is truly private
  2. A usable API is inherently public
  3. Can overwrite API functions to 'advise' or replace
#1 (duck|monkey)(punching|patching) RULE:
Respect the original API

  • Receive the same arguments
  • Return value of the same type

Unit Testing:

Minutes Now Will Save Hours Later



Test:

  • Exposed APIs
  • State changes

Helpers:

mockjax, QUnit, PhantomJS (jasmine?)

How to Test?

Handling Closures:

  • Pass in an object
  • Attach testable functions/state
  • ...
  • Profit

Adding Tests to Existing Code Base:

  • Write tests for new code
  • Write tests for modification of old code

Automated Testing

  • Grunt(watch code and validate on the fly)



Building Single Page Apps


  • use a router and clean up after yourself
  • anti-leakage patterns
    • release event listeners use event namespacing to make it easy
    • remember to destroy on router.exit

Future of Selectors

timmy wilson, writer of swizzle

  • CSS3 Modules recommendation: 2012
  • CSS4 working draft: current

querySelectorAll() - almost jQuery() (scoped to document)
<parent>
    <style scoped>
        @import url(whatever);/*sweet*/
        ul li { background: blue; }
    </style>
    <ul><li>blue</li></ul>
</parent>
<ul><li>notblue</li></ul>
Allows scoped css styles (support sucks though)
IE 11, FF21, Chrome 28 Safari 7

CSS4

Selectors Level 4 Highlights

:matches() /* 'or' group your selectors (only compound selectors, no combinators)*/
a:link, a:visited, a:hover, a:active
/* becomes */ a:matches(:link, :visited, :hover, :active)
div#main, div.animated, div.visible
/* becomes */ div:matches(.animated, .visible, #main)
:not(:link, :visited) //opposite of matches
Reference Combinators
label /for/ input { display: none }<label for="someid">hidden</label><input id="someid"/>
Case-insensitive attributes
 .superman[data-power='flying' i]
:any-link, :local-link
:any-link { color: blue; } :local-link { color: red; } 
:local-link(3) { color: orange;} /* number is for depth http://x.com/1/2/3/ */

(More) selector level 4

:blank
<style>:blank { color: red;} :empty { color: blue; } 
<p>     </p><!-- ← red --><p></p><!-- ← blue -->
:nth[-last]-match(An+B of selector)
p.foo:nth-match(even) /* !!! */
option:nth-match(-1n+4 of :selected) /* first four */
Subject Selector (! or $)?
/* sets the background of the containing li on hover */
nav > ul > $li > a:hover { background: gray }/* add shadow to the containing ul on hover */
nav > $ul > li > a:hover { box-shadow: 0 0 10px black }
:scope (same as <style scoped>)
<p id="outside-scope">not green</p>
<section>
<style scoped> p {background: green;}; /*or*/ :scope p {background: green;}</style>
<p>is green!</p>
</section><script>section.querySelectorAll(':scope p');//super fast!</script>


Scoped and jQuery

querySelectorAll() now has scope
  • Improves jquery selector speed
  • Limited to descendants


Introducing findAll()

jQuery.find = function(selector, context) {
    return context.findAll(selector);
}



Fixing Broken Windows

The outward appearance of criminal behavior can lead to actual criminality

So it's even better to stop windows from breaking in the first place

Problems:
Mixed spaces and tabs, trailing whitespace




    • Avoid monolithic files
    • Embrace long term laziness
    • Modules!
    • When writing require.js
    • Leave $.fn alone unless you really mean it
    • Prefer simple dependencies over complex
    • Dead Code (kill it)



    Write testable code

    Avoid anonymous doc-ready callbacks

    don't retest jquery
    Use testswarm

    Don't abuse globals

    Implicit globals "use strict"

    Beyond dom Manipulation

    crashlytics 'how we made our app'




    AB Testing

    http://www.wired.com/business/2012/04/ff_abtesting/
    http://www.howardism.org/Technical/JavaScript/A-B-Testing.html
    not too useful or easy to do for our market

    Front/Back End Node.js

    • Express framework (MVC client+server)
    • Jade templating
    • Stylus CSS preprocessor

    Express
    npm install express && express PROJECT_NAME --css stylus && cd PROJECT_NAMEnpm install && node app.js & links http://localhost:3000
    Jade
    Stylus
    • Expressive, dynamic, robust css
    • Takes less/sass one step further
    • Use grunt for automation

    Bug Wild

    lawnsea/bugwild


    Any software large enough
    to be interesting has bugs.

    The industry average is 15 - 50 errors/kloc1.

    1) Steve McConnell. Code Complete, 2nd Edition, 2004.



    Tests demonstrate correct performance of specific tasks under highly constrained circumstances.
    They do not prove the absence of bugs.


    In fact, your users will encounter precisely those bugs that you did not write tests for.

    Even if you had 100% coverage, (which you don't) testing cannot prove correctness.



    Notify the users of failures as soon as possible, or their bug reports will often be unhelpful and misleading.


    Always have an onerror handler

    This is your last line of defense.

    If you get here, you are crashing.

    In an onerror handler, you get the error message and the line number and URL where the error originated.

    window.onerror = function (message, url, line) {
      ExampleApp.crash(message, url + '\n' + 'line: ' + line);
    };



    The rules of exception handling

    1. Never catch an exception you can't do anything about.

    2. If you catch an exception you cannot handle,
      always re-throw the same exception.

    3. If you catch an exception in your top-level code that you 
      cannot handle, crash.


    Never throw in a callback.

    especially yui!

    Wrap code that can throw in try/catch blocks.


    function safeCallback(fn) {
      return function () {
        try {
          return fn.apply(this, arguments);
        } catch (e) {
          ExampleApp.crash(e.name + ': ' + e.message, e.stack);
        }
      };
    }
    




    When using callbacks, always provide an error handler


    $.ajax('http://api.example.com/foo/bar', {
      success: safeCallback(function (result) {
        // ...
      }),
      error: safeCallback(function (jqXHR, textStatus, errorThrown) {
        // ...
      })
    });
    



    Always provide a fail handler when using promises


    $.ajax('http://api.example.com/foo/bar').
      done(safeCallback(function (result) {
        // do something cool...
      })).
      fail(safeCallback(function (jqXHR, textStatus, errorThrown) {
        // handle the error...
      }));
    

    How do I get a report?

    Email - Requires User Action (copy/paste)

    Web Form - More Complicated, requires server support

    Automatic Reports - No feedback

    Send an automatic report immediately.

    • Provide user with a link to a feedback form
    • Attach feedback to initial report
    • Airbrake
    • Debuggify
    • Errorception
    • JSLogger
    • Muscula
    • Sentry - Finally one that's not only a service

    MAD SCIENCE

    Reanimator

    The Idea:

    Computers are deterministic: given the same inputs, they will always arrive at the same result.

    Capture input to a log.

    Later, run the program again and replay the captured input.

    Made with Slides.com