who am i what is jquery jquery s core philosophy get some
play

who am I? what is jquery? jquerys core philosophy get some - PowerPoint PPT Presentation

who am I? what is jquery? jquerys core philosophy get some elements. do some stuff. get some elements. but how? why reinvent the wheel? CSS 3 plus div div:first div:animated div:last div:contains( div#foo txt)


  1. who am I?

  2. what is jquery?

  3. jquery’s core philosophy

  4. get some elements. do some stuff.

  5. get some elements. but how?

  6. why reinvent the wheel?

  7. CSS 3 plus • div • div:first • div:animated • div:last • div:contains( • div#foo txt) • div:not(#foo) • div.class • div:empty • div:even • div, p, a • div:has(p) • div:odd • div p • div:parent • div:eq(1) • div > p • div:hidden • div:gt(1) • div + p • div:visible • div:lt(1) • div ~ p • div:header

  8. CSS 3 plus • div • div:first • div:animated • div:last • div:contains( • div#foo txt) • div:not(#foo) • div.class • div:empty • div:even • div, p, a • div:has(p) • div:odd • div p • div:parent • div:eq(1) • div > p • div:hidden • div:gt(1) • div + p • div:visible • div:lt(1) • div ~ p • div:header

  9. CSS 3 plus • div[foo] • :last-child • :button • div[foo=bar] • :only-child • :file • div[foo!=bar] • :input • :hidden • div[foo^=bar] • :text • :enabled • div[foo$=bar] • :password • :disabled • div[foo*=bar] • :radio • :checked • :nth-child(2) • :checkbox • :selected • :nth- • :submit child(even) • :image • :first-child • :reset

  10. get some elements.

  11. $(“table tr:nth- child(even) > td:visible”)

  12. do stuff.

  13. $(“div”) Returns jquery object

  14. $(“div”).fadeIn() Returns jquery object

  15. $(“div”).fadeIn() .css(“color”, “green”) Returns jquery object

  16. we call this chaining

  17. Crazy chains $(“ul.open”) // [ ul, ul, ul ] .children(“li”) // [ li, li, li ] .addClass(“open”) // [ li, li, li] .end() // [ ul, ul, ul ] . fi nd(“a”) // [ a, a, a ] .click(function(){ $(this).next().toggle(); return false; }) // [ a, a, a ] .end(); // [ ul, ul, ul ]

  18. 5 parts of jquery dom events effects ajax plugins

  19. $(“div”) <body> <div> <div> <pre> <p> <p> <p> <p> <p> <p> dom

  20. $(“div”).next() <body> <div> <div> <pre> <p> <p> <p> <p> <p> <p> dom

  21. $(“div”).nextall(“p”) <body> <div id='foo'> <p> <p> <pre> <p> dom

  22. $(“div”).nextall( “p:first”) <body> <div id='foo'> <p> <p> <pre> <p> dom

  23. find dom

  24. $(“div pre”) <body> <div> <div> <pre> <p> <pre> <pre> <p> <pre> <p> dom

  25. $(“div”).find(“pre”) <body> <div> <div> <pre> <p> <pre> <pre> <p> <pre> <p> dom

  26. filter $(“div:contains(some text)”) $(“div”).filter( “:contains(some text)”) dom

  27. filter $(“div”).filter(function() { return $(this).text() .match(“some text”) }) dom

  28. andself() dom

  29. $(“div”).parent() .andself() <body> <div id='foo'> <div> <pre> <p> <p> <p> <p> <p> <p> dom

  30. attributes Getter $(“div”).attr(“id”) $(“div”).attr(“id”, “hello”) Setter dom

  31. attributes $(“div”).attr(“id”, function() { return this.name }) $(“div”).attr( {id: “foo”, name: “bar”}) dom

  32. other Getter $(“div”).html() $(“div”).html(“<p>Hello</p>”) Setter dom

  33. other Getter $(“div”).text() $(“div”).text(“Hello”) Setter dom

  34. other Getter $(“div”).val() $(“div”).val(“Hello”) Setter dom

  35. noticing a pattern?

  36. 5 parts of jquery dom events effects ajax plugins

  37. bind $(“div”).bind(“click”, function() { ... }) Alias: $(“div”).click(function() { ... })

  38. “this” refers to the element bound

  39. e $(“div”).click(function(e) { ... })

  40. corrected event object Property Correction target The element that triggered the event (event delegation) relatedTarget The element that the mouse is moving in (or out) of pageX/Y The mouse cursor relative to the document which mouse: 1 (left) 2 (middle) 3 (right) keypress: The ASCII value of the text input metaKey Control on Windows and Apple on OSX

  41. trigger $(“div”).trigger(“click”) Alias: $(“div”).click()

  42. triggerhandler doesn’t trigger the browser’s default actions

  43. custom events $(“div”).bind(“myEvent”, function() { ... }) $(“div”).trigger(“myEvent”)

  44. namespaces $(“div”) .bind(“activate.autocomplete”, function() { ... }) $(“div”).trigger(“activate”)

  45. 5 parts of jquery dom events effects ajax plugins

  46. make easy things easy $(“div”).load(“some_url”); $(“div”).load(“some_url”, {data: “foo”},

  47. it’s easy to do it right $.getJSON(“some_url”, function(json) { ... }) $.getJSON(“some_url”, {data: “foo”}, function(json) { ... })

  48. it’s consistent $.get(“some_url”, function(text) { ... }) $.post(“some_url”, {data: “foo”}, function(text) { ... })

  49. and powerful $.ajax Options • async • global • beforeSend • ifModified • cache • jsonp • complete • processData • contentType • success • data • timeout • dataType • type • error

  50. and powerful global ajax settings /* No Ajax requests exist, and one starts */ $(“div.progress”).ajaxStart(function() { $ (this).show() }); /* The last Ajax request stops */ $(“div.progress”).ajaxStop(function() { $ (this).hide() }); /* Any Ajax request is sent */ $(“p”).ajaxSend(function() { ... }); /* Any Ajax request completes (success or failure) */ $(“div”).ajaxComplete(function() { ... }); /* Any Ajax request errors out */ $(“div”).ajaxError(function() { ... }); /* Any Ajax request succeeds */ $(“div”).ajaxSucccess(function() { ... });

  51. 5 parts of jquery dom events effects ajax plugins

  52. metadata

  53. metadata <input class="autocomplete" metadata="{ cluster_id: 1, get: '/customers/from_salesforce’ fields: {id: 'details[salesforce_id]'} }" />

  54. in rails <%= autocomplete( “Search for Salesforce Customer”, { :cluster_id => @cluster.id, :get => “/customers/from_salesforce”, :fields => {:id => ‘details[salesforce_id]’} } ) %>

  55. livequery

  56. replaces each metadata $("input.autocomplete").each(function() { var match = $(this).metadata().match || "name"; $(this).autocomplete({ ajax: $(this).metadata().get, insertText: function(obj) { return obj[match]; }, match: function(typed) { var regex = new RegExp(typed, “i”); return this[match || "name"] .toString().match(regex); }, template: function(obj) { return "<li>" + obj[match || "name"] + "</li>"; } }) });

  57. replaces each $("input.autocomplete").each(function() { metadata <%= autocomplete( var match = $(this).metadata().match || "name"; $(this).autocomplete({ “Search for Salesforce Customer”, { ajax: $(this).metadata().get, :cluster_id => @cluster.id, insertText: function(obj) { return obj[match]; }, :get => “/customers/from_salesforce”, match: function(typed) { var regex = new RegExp(typed, “i”); :fields => {:id => return this[match || "name"] reminder ‘details[salesforce_id]’} .toString().match(regex); }, } template: function(obj) { return "<li>" + obj[match || "name"] + "</ ) %> li>"; } }) });

  58. replaces each catch that? $("input.autocomplete").livequery(function() { var match = $(this).metadata().match || "name"; $(this).autocomplete({ ajax: $(this).metadata().get, insertText: function(obj) { return obj[match]; }, match: function(typed) { var regex = new RegExp(typed, “i”); return this[match || "name"] .toString().match(regex); }, template: function(obj) { return "<li>" + obj[match || "name"] + "</li>"; } }) });

  59. replaces bind $("a.edit_autocomplete").bind("click", function() { $(this).parent().hide() .prev().show() .find("input").focus(); return false; });

  60. replaces bind catch that? $("a.edit_autocomplete").livequery("click", function() { $(this).parent().hide() .prev().show() .find("input").focus(); return false;

  61. seamless interaction

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend