Created by Gordon L. Hempton / @ghempton / GroupTalent / epf.io
App.Adapter = Ep.RestAdapter.extend({
namespace: 'api' // optionally set a prefix for all urls
});
App.Group = Ep.Model.extend({
name: Ep.attr('string'),
users: Ep.hasMany(App.User)
});
App.User = Ep.Model.extend({
name: Ep.attr('string'),
group: Ep.belongsTo(App.Group)
});
session.load(App.User, 1);
session.load('user', 1); // string version
session.load('user', 1).then(function(user) {
// user will be loaded
});
session.create(App.User, {name: 'herp'});
session.create('user', {name: 'herp'}); // alternative string version
session.flush().then(function() {
// changes will have been persisted
});
user.set('name', 'embereno');
session.flush().then(function() {
// changes will have been persisted
});
// updates can happen while a flush is pending
user.set('name', 'kris');
var group = session.create('group', {name: 'emberenos'});
user.set('group', group);
// inverse relationships automatically updated
group.get('users'); // [user]
var group = session.create('group', {name: 'emberenos'});
var user = session.create('user', {name: 'dave'});
user.set('group', group);
group.get('users').pushObject(existingUser);
session.flush().then(function() {
// all changes will be persisted
});
var user = session.load(App.User, 1);
// this create a "fork" of the session
var childSession = session.newSession();
// this user will be a separate instance from `user`
var childUser = childSession.load(App.User, 1);
user === childUser; // false, they are separate instances
user.isEqual(childUser); // true, key based comparison
childUser.name = 'derp'; // this will not affect `user`
// flush changes immediately to the parent session and server
childSession.flush();
$.getJSON('/users/1', function(data) {
var user = App.User.create(data);
session.merge(user);
});
var socket = io.connect('http://localhost');
socket.on('user', function (data) {
var user = App.User.create(data);
session.merge(user);
});
REST vs. Sockets
Realtime?
by Neil Fraser
Gordon L. Hempton / @ghempton / GroupTalent / epf.io
Tom Dale, Yehuda Katz, Stefan Penner, and all the ED contributors
Ray Bunnage and heartsentwined for early adoption