Eric Ferraiuolo![]()
--I'll write more here soon--
Features Custom Event hooks for each instance (fired for every request) and per-request callbacks, convenience methods for HTTP verbs, entity translations (i.e. Object/Array -- JSON, JSON -- Object/Array) based on Content-Type header, parameter convenience.
<script src="http://yui.yahooapis.com/3.1.0/build/yui/yui-min.js"></script>// *** Simple Example *** //
YUI({
//Last Gallery Build of this module
gallery: 'gallery-2010.03.30-17-26'
}).use('gallery-resource', function(Y) {
var r = new Y.Resource({ uri: 'path/to/resource/{id}' });
r.GET({ // makes request to: path/to/resource/1?format=json
params : { id: 1, format: 'json' },
on : {
success : function(e){
// e.entity is an Object parsed from the JSON returned by the server
}
}
});
});
// *** Complex Example *** //
YUI({
//Last Gallery Build of this module
gallery: 'gallery-2010.03.30-17-26'
}).use('gallery-resource', function(Y) {
var r = new Y.Resource({
uri : 'path/to/resource/{id}',
headers : {
'Content-Type' : 'application/json',
'Accept' : 'application/json'
},
on : {
request : logRequest,
deleteRequest : function(e) {
e.preventDefault(); // umm, no, you can't do that!
},
response : logResposne,
getSuccess : logParsedJSONEntity
}
});
r.DELETE({ params: { id: 1 } }); // will be prevented
r.GET({ params: { id: 1, format: 'json' } }); // io request to: path/to/resource/1?format=json
r.GET({
params : { id: 2 },
on : {
success : function(e){
Y.log('Only called for this request', 'info');
}
}
});
function logRequest (e) {
Y.log('request params: '+e.params, 'info');
}
function logResponse (e) {
Y.log('response status: '+e.response.status, 'info');
Y.log('request parmas: '+e.request.params, 'info');
}
function logParsedJSONEntity (e) {
var entity = e.entity;
Y.log('parsed JSON: '+(Y.Lang.isObject(entity) || Y.Lang.isArray(entity)), 'info');
}
});
No forum posts for this module.
© 2010 YUI Library - Site Credits