Kevnz
8/25/2013 - 10:27 AM

Example of the default behavior. Taken from http://yuilibrary.com/yui/docs/model/#restful-xhr

Example of the default behavior. Taken from http://yuilibrary.com/yui/docs/model/#restful-xhr

// Create `Y.User`, a `Y.Model` subclass and mix-in the RESTful XHR sync layer.
Y.User = Y.Base.create('user', Y.Model, [Y.ModelSync.REST], {
    // The root or collection path segment for the server's Users resource.
    root: '/users'
});

var existingUser = new Y.User({id: '1'}),
    oldUser      = new Y.User({id: '2'}),
    newUser      = new Y.User({name: 'Eric Ferraiuolo'});

// GET the existing user data from: "/users/1"
existingUser.load(function () {
    Y.log(existingUser.get('name')); // => "Ron Swannson"

    // Correct the user's `name` and PUT the updated user at: "/users/1"
    existingUser.set('name', 'Ron Swanson').save();
});

// DELETE the old user data at: "/users/2"
oldUser.destroy({remove: true});

// POST the new user data to: "/users"
newUser.save(function () {
    // The server can return the user data with an `id` assigned.
    Y.log(newUser.get('id')); // => "3"
});