Changeset 701

Show
Ignore:
Timestamp:
04/23/06 20:05:06 (2 years ago)
Author:
bob@redivi.com
Message:

checkin

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • presentations/2006/ajax_experience/slides.txt

    r696 r701  
    329329* If you don't use itertools in Python, you probably won't directly use 
    330330  much of MochiKit.Iter either 
     331 
     332 
     333MochiKit.DateTime 
     334================= 
     335 
     336* JavaScript Date objects aren't very convenient 
     337* W3C profile ISO 8601 style timestamps are Good 
     338 
     339Dates 
     340===== 
     341 
     342isoDate(str): 
     343    Convert ISO 8601 date to a Date object 
     344 
     345toISODate(date): 
     346    Convert a Date object to an ISO 8601 date 
     347 
     348americanDate(str): 
     349    Convert an MM/DD/YYYY date to a Date object 
     350 
     351toAmericanDate(date): 
     352    Convert a Date object to a M/D/YYYY date (1/1/2001) 
     353 
     354toPaddedAmericanDate(date): 
     355    Convert a Date object to a MM/DM/YYYY date (01/01/2001) 
     356 
     357 
     358Time and Timestamps 
     359=================== 
     360 
     361isoTimestamp(str): 
     362    Convert a YYYY-MM-DD hh:mm:ss or YYYY-MM-DDThh:mm:ssZ timestamp to a 
     363    Date object 
     364 
     365toISOTime(date): 
     366    Convert a Date object to a hh:mm:ss string 
     367 
     368toISOTimestamp(date, realISO=false): 
     369    Convert a Date object to a YYYY-MM-DD hh:mm:ss string.  If realISO is true, 
     370    then use the proper YYYY-MM-DDThh:mm:ssZ form. 
     371 
     372 
     373MochiKit.Format 
     374=============== 
     375 
     376* JavaScript doesn't really do string formatting 
     377* Users like to see big numbers with thousands separators 
     378* Number formatting based on Java's Number Format Pattern Syntax 
     379 
     380 
     381Simple String Mangling 
     382====================== 
     383 
     384strip(str, chars="\\s"): 
     385    Return a string based on str with leading and trailing whitespace stripped. 
     386    If the chars regex is specified, it will be used instead of the default. 
     387 
     388lstrip(str, chars="\\s"): 
     389    strip only leading whitespace 
     390 
     391rstrip(str, chars="\\s"): 
     392    strip only trailing whitespace 
     393 
     394 
     395Number Formatting 
     396================= 
     397 
     398numberFormatter(pattern, placeholder="", locale="default"): 
     399    Return a function that formats numbers using the given pattern 
     400 
     401Examples:: 
     402     
     403    >>> dollarFormat = numberFormatter("$###,###.##") 
     404    >>> dollarFormat(1234567.89) 
     405    "$1,234,567.89" 
     406     
     407    >>> percentFormat = numberFormatter("###,###%") 
     408    >>> percentFormat(123.45) 
     409    "12,345%" 
     410 
     411 
     412MochiKit.Logging 
     413================ 
     414 
     415* alert() sucks 
     416* Debugging is hard enough 
     417* Not every browser has FireBug 
     418* I've had no luck with Venkman 
     419* Less luck with Microsoft's Script Debugger 
     420 
     421 
     422Simple logging 
     423============== 
     424 
     425log(msg): 
     426    Logs a message at the INFO level 
     427 
     428logDebug(msg): 
     429    Logs a message at the DEBUG level 
     430 
     431logWarning, logError, logFatal... 
     432 
     433 
     434Where'd my log messages go? 
     435=========================== 
     436 
     437* In Safari, Firefox with FireBug, and Opera they will be logged to the 
     438  appropriate console (but you can turn this off). 
     439* Will also go to any logging listener 
     440* Logging listeners are just functions that take log message objects 
     441 
     442 
     443Bookmarklet Debugging 
     444===================== 
     445 
     446Just bookmark this:: 
     447 
     448    javascript:MochiKit.Logging.logger.debuggingBookmarklet() 
     449 
     450Currently, this is a pop-up MochiKit.LoggingPane. 
     451 
     452 
     453MochiKit.LoggingPane 
     454==================== 
     455 
     456* A usable MochiKit.Logging listener 
     457* Can be used in-line or as a pop-up window 
     458* Could be fancier, but it gets the job done 
     459 
     460 
     461Manually creating a LoggingPane 
     462=============================== 
     463 
     464Pop-up:: 
     465 
     466    createLoggingPane() 
     467 
     468Inline:: 
     469 
     470    createLoggingPane(true) 
     471 
     472 
     473MochiKit.DOM 
     474============ 
     475 
     476* DOM is painful, but it doesn't have to be 
     477* Functions for finding, creating, modifying DOM nodes 
     478* Most functions will take either a string or a DOM node reference, saving 
     479  you the trouble of getElementById(s), $(s) or similar. 
     480 
     481 
     482createDOM 
     483========= 
     484 
     485* Does a good job of turning any object into a DOM node 
     486* Strings and numbers to text, flattens Arrays or iterators of anything 
     487* Extensible with an adapter registry 
     488 
     489 
     490createDOM example 
     491================= 
     492 
     493createDOM(tagName, attributes, contents...) 
     494 
     495A simple list:: 
     496 
     497    var node = createDOM("ul", null, 
     498        createDOM("li", null, "first item"), 
     499        createDOM("li", null, "second item")); 
     500 
     501Equivalent to:: 
     502 
     503    <ul> 
     504        <li>first item</li> 
     505        <li>second item</li> 
     506    </ul> 
     507 
     508 
     509But that's still ugly... 
     510======================== 
     511 
     512We use aliases instead, for all of the common tags:: 
     513 
     514    var node = UL(null, 
     515        LI("first item"), 
     516        LI("second item")); 
     517 
     518Note that MochiKit 1.3 and later allows a text node as the first 
     519parameter for convenience (saves a lot of "null" typing) 
     520 
     521 
     522Flattening lists 
     523================ 
     524 
     525Functional programming comes in handy for DOM creation:: 
     526 
     527    var items = ["first item", "second item"]; 
     528    var node = UL(null, map(LI, items)); 
     529 
     530 
     531What about attributes? 
     532====================== 
     533 
     534First parameter is either an object (attributes), or a string (text node):: 
     535 
     536    var classes = repeat({"class": "myitemclass"}); 
     537    var items = ["first item", "second item"]; 
     538    var node = UL({"class": "mylistclass"}, 
     539        map(LI, classes, items)); 
     540 
     541 
     542Alternating classes 
     543=================== 
     544 
     545More MochiKit.Iter goodies, good for table rows:: 
     546 
     547    var classes = cycle({"class": "even"}, {"class": "odd"}); 
     548    var items = ["first item", "second item"]; 
     549    var node = UL(null, map(LI, classes, items)); 
     550 
     551 
     552Scraping text 
     553============= 
     554 
     555Scraping text is useful for progressive enhancement... 
     556 
     557HTML:: 
     558 
     559    <span id="scrape_me">text is <b>here</b></span> 
     560 
     561JavaScript:: 
     562 
     563    >>> scrapeText("scrape_me"); 
     564    "text is here" 
     565 
     566 
     567Reading forms 
     568============= 
     569 
     570HTML:: 
     571 
     572    <form id="formNode"> 
     573        <input type="hidden" name="foo" value="1" /> 
     574        <input type="hidden" name="bar" value="2" /> 
     575    </form> 
     576 
     577JavaScript:: 
     578 
     579    >>> formContents("formNode") 
     580    [["foo", "bar"], ["1", "2"]] 
     581 
     582 
     583Manipulating DOM nodes 
     584====================== 
     585 
     586appendChildNodes(parentNode, childNode...): 
     587    Add nodes to parentNode using the DOM coercion rules from createDOM 
     588 
     589replaceChildNodes(parentNode, childNode...): 
     590    Remove all children from parentNode, then appendChildNodes 
     591 
     592swapDOM(dest, src): 
     593    Replace dest with src (if src is null, then dest is removed) 
     594 
     595 
     596Manipulating DOM attributes 
     597=========================== 
     598 
     599setNodeAttribute(node, attr, value): 
     600    Set the node attribute attr=value  
     601 
     602updateNodeAttributes(node, attrs): 
     603    Set the node attributes based on the object attrs 
     604