Skip to content

Instantly share code, notes, and snippets.

@ericf
Created May 11, 2010 17:56
Show Gist options
  • Select an option

  • Save ericf/397606 to your computer and use it in GitHub Desktop.

Select an option

Save ericf/397606 to your computer and use it in GitHub Desktop.
YUI().use('base', 'gallery-resource', function(Y){
var MyClass, myClass, resource;
// MyClass Def
MyClass = function (config) {
MyClass.superclass.constructor.apply(this, arguments);
};
Y.extend(MyClass, Y.Base, {
initializer : function () {
this.after('fooChange', function(e){
var foo = e.newVal;
this.get('resource').PUT({
params : foo.id,
entity : foo
});
});
}
}, {
NAME : 'myClass',
ATTRS : {
foo : {
validator: Y.Lang.isObject
},
resource : {
validator : function (v) {
return ( v && Y.Lang.isFunction(v.sendRequest) );
}
}
}
});
// Resource instance
resource = new Y.Resource({
uri : 'some/path/{id}', // {id} is substituded
headers : {
'Content-Type' : 'application/json', // causes JSON serialization on request entity
'Accept' : 'application/json' // causes JSON deserialization on response entity
},
on : {
putSuccess : function(e){
Y.log('saved to server'); // logs to the console on every save
}
}
});
// MyClass instance
myClass = new MyClass({ resource: resource });
// Set and save foo to server
myClass.set('foo', {
id : '1',
bar : 'bar',
date : new Date().getTime()
});
// PUT request will be made to some/path/1 with a JSON entity body
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment