How it works
When your page loads, Glass.js attaches objects that represent your Rails models
in the window
object under the glass
namespace. So when you have a User
model in your Rails backend, Glass.js will create glass.User
object that you
can access from anywhere in your JavaScript code.
Glass.js exposes functions such as find
, findAll
, create
, update
and
delete
to handle XMLHTTPRequests to your backend, simplifying your CRUD API
calls.
Please read Glass to learn more about the setup instructions and usage of the gem that uses this JavaScript library.
The following usage examples makes use of the Glass API given that you have a
User
model in your Rails app and you have configured the Glass gem to expose it.
Finds a list of records in a model with 'Foo' as name.
glass.User.find({
name: 'Foo'
}, function (res, error) {
if (!error) {
// Do something with res
}
});
The following usage example finds all users.
glass.User.find(function (res, error) {
if (!error) {
// Do something with res
}
});
Create a new user record.
var user = {
name: 'Jaune Sarmiento',
email: '[email protected]'
};
glass.User.create(user, function (res, error) {
if (!error) {
// Do something with res
}
});
Update the user with id == 1
and update its name to Joko
.
// Given our create() function returns the user object with 1 as id
var user = {
id: 1,
name: 'Joko'
};
glass.User.update(user, function (res, error) {
if (!error) {
// Do something with res
}
});
Delete a user record with id == 1
.
glass.User.delete(1, function (res, error) {
if (!error) {
// Do something with res
}
});