Search

12/10/2007

yui code snippet

The Yahoo! User Interface Library. Simon Willison. XTech Ajax Developer’s Day
Preparatory notes for "The Yahoo! User Interface Library" at XTech
getElementsBy which is more general, taking an acceptance function that is passed each node in turn and must return true or false:

YAHOO.util.Dom.getElementsBy(function(el) {
return (/^http:\/\/www\.yahoo\.com/.test(el.getAttribute('href')));
}));

YAHOO.util.Connect.asyncRequest(
'GET', '/ajaxy-goodness', {
success: function(o) {
alert(o.responseText);
},
failure: function(o) {
alert('Request failed: ' +o.statusText);
}
}
);

As you can see, you pass in an object containing success and failure callback functions. The connection object can also handle scope correction, for example if you want to call methods on a JavaScript object:

YAHOO.util.Connect.asyncRequest('GET', '/ajaxy-goodness', {
success: myObject.onSuccess,
failure: myObject.onFailure,
scope: myObject
});

Custom Event
var myEvent = new YAHOO.util.CustomEvent(
'myEvent'
);
myEvent.subscribe(function() {
alert('event fired');
});
myEvent.fire();


function onSuccess(o) {
alert(o.argument);
}

YAHOO.util.Connect.asyncRequest('GET', '/ajaxy-goodness', {
success: onSuccess,
argument: 'some extra data'
});

onavailable
YAHOO.util.Event.onAvailable(
'mydiv', function() {
alert('mydiv has become available');
}
);

Extra callback arguments
function msgAlert(e, msg) {
alert(msg);
}
YAHOO.util.Event.on(
'mydiv', 'click', msgAlert, "My div was clicked"
);

Reference:
Preparatory notes for "The Yahoo! User Interface Library" at XTech

沒有留言: