Created
May 11, 2010 17:56
-
-
Save ericf/397606 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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