Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ajax Best Practices

Ajax Best Practices

Given at Kansas City Developers Conference 2013

Garrett Pennington

May 04, 2013
Tweet

More Decks by Garrett Pennington

Other Decks in Programming

Transcript

  1. Customers who bought this item also bought Clean Javascript: Taming

    the Unicorn Patrick Delancy Testable Javascript James Kovacs Typescript: Beyond Javascript Jake Adams Graphic Design for Programmers Me!
  2. the jQuery object jQuery = $ (mostly) Array-like set of

    matched DOM elements jQuery function always returns a jQuery object
  3. Early Applications Outlook Web Access (2000) Gmail (2004) Google Maps

    (2005) Brought Desktop functionality to the web
  4. jQuery Ajax Advantages Takes care of x-browser quirks for you

    Simple API Disadvantage You’ll need to load jQuery Different jQuery methods (getJSON, load, etc-- all wrappers)
  5. Ajax request returns JSON Return all properties of a record

    You can do more with it: HTML,Tables, SVG, etc Object primary keys are included
  6. Ajax request returns JSON Create a boilerplate response that includes:

    Success Codes Human Readable Messages Request Time
  7. Ajax request returns JSON What does ‘error’ mean? What if

    the query fails? What if the results are unexpected? What if the request isn’t authorized?
  8. DOM elements are constructed in client JSON provides us with

    all the data we need! Sometimes creating DOM elements is okay: Small values Upvotes, timestamps, table cell values
  9. DOM elements are constructed in client Slow and complicated to

    construct client-side User could be viewing site on a potato Rendering markup in 2 places! Markup and JS are dependent on each other
  10. Create an API Private or public If its useful for

    you, then its useful for someone else Multi-use: Website Native apps Mashups
  11. You need your REST Resource-Based Individual resources are identified in

    requests using URIs as resource identifiers. The resources themselves are conceptually separate from the representations that are returned to the client. For example, the server does not send its database, but rather, some HTML, XML or JSON that represents some database records expressed, for instance, in Finnish and encoded in UTF-8, depending on the details of the request and the server implementation. Manipulation of Resources Through Representations When a client holds a representation of a resource, including any metadata attached, it has enough information to modify or delete the resource on the server, provided it has permission to do so. Self-descriptive Messages Each message includes enough information to describe how to process the message. For example, which parser to invoke may be specified by an Internet media type (previously known as a MIME type). Responses also explicitly indicate their cache-ability. Hypermedia as the Engine of Application State (HATEOAS) Clients deliver state via body contents, query-string parameters, request headers and the requested URI (the resource name). Services deliver state to clients via body content, response codes, and response headers. This is technically referred-to as hypermedia (or hyperlinks within hypertext). www.restapitutorial.com/lessons/whatisrest.html
  12. You need your REST Build an API that: Resource Based

    Verb is in HTTP Method GET, POST, UPDATE, DELETE Include in response the URI of the resource itself API delivers state in header: HTTP 201 - Created HTTP 204 - Deleted
  13. Multiple instances of ajax code One ajax method per resource,

    maybe more Mostly, the code looks the same Let's DRY off
  14. One generic instance of ajax code One(ish) ajax method Return

    parsers in response Attach parser to namespaced object
  15. Switch between contexts with synchronous requests context/resource The web is

    still synchronous-- it's okay Your resource based url structure hints at context separations
  16. Somewhere between Web 3.0 and 5.0? All about Data Profitable

    (1.0) entities that Connect (2.0) us Now let’s do something interesting with all that data
  17. “Mobile First” Design philosophy that starts with the most important,

    necessary content and enhances as the client's capabilities expand.
  18. How about “Data First” Design philosophy that starts with data

    and expands to presentation if necessary. Serialized data by default. Maybe get a richer mimetype because of: User agent in header ?format=html
  19. Testing You can use your standard test runner to test

    ajax uses Check for “X-Requested-With” header = "XMLHttpRequest”