Search

12/05/2007

YUI 2.4

YUI CSS Selector
Yahoo! UI Library: Selector Utility
cheatsheet

" " Descendant Combinator: “A B”represents an element B that has A as an ancestor.
> Child Combinator: “A > B” represents anelement B whose parent node is A.
+ Direct Adjacent Combinator: “A + B”represents an element B immediately following a sibling element A.
~ Indirect Adjacent Combinator: “A ~ B”represents an element B following (not necessarily immediately following) a sibling element A.


JSON utility - 把 eval() 丟掉吧
// Potentially UNSAFE
var data = eval('(' + shouldBeJsonData + ')');

// Safe
var data = YAHOO.lang.JSON.parse(shouldBeJsonData);

JSON to JavaScript Data
var jsonString = '{"productId":1234,"price":24.5,"inStock":true,"bananas":null}';

// Parsing JSON strings can throw a SyntaxError exception, so we wrap the call
// in a try catch block
try {
var prod = YAHOO.lang.JSON.parse(jsonString);
}
catch (e) {
alert("Invalid product data");
}

// We can now interact with the data
if (prod.price < 25) {
prod.price += 10; // Price increase!
}

JavaScript to JSON
var myData = {
puppies : [
{ name: "Ashley", age: 12 },
{ name: "Abby", age:9 }
]
};

var jsonStr = YAHOO.lang.JSON.stringify(myData);

// jsonStr now contains the string (without the leading comment slashes)
// {"puppies":[{"name":"Ashley","age":12},{"name":"Abby","age":9}]}

// Create a cyclical reference
myData["puppies"][0]["NEWKEY"] = myData;

jsonStr = YAHOO.lang.JSON.stringify(myData);
// jsonStr now contains the string (without the leading comment slashes)
// {"puppies":[{"name":"Ashley","age":12,"NEWKEY":null},{"name":"Abby","age":9}]}

沒有留言: