Skip to content

Instantly share code, notes, and snippets.

@joeytrapp
Created August 11, 2014 16:48
Show Gist options
  • Save joeytrapp/65890927942b7725e60d to your computer and use it in GitHub Desktop.
Save joeytrapp/65890927942b7725e60d to your computer and use it in GitHub Desktop.
Example of pretender fixtures that can be run in the browser and queried against with jQuery.
// visit localhost:3000/test/demo.html
// run this snippet from chrome dev tools sources panel
agent = new Agent();
userIds = agent.makeIds('users', 2);
agent.group('users', function() {
this.fixture(userIds[1], { first: 'Joey', last: 'Trapp' });
this.fixture(userIds[2], { first: 'Lee', last: 'Forkenbrock' });
});
agent.build();
agent.server(function(a) {
a.router.get('/users');
a.router.get('/users/:id');
a.router.post('/users');
a.router.put('/users/:id');
a.router.delete('/users/:id');
// or use the resource method that defines all the methods above
// a.router.resource('users');
});
// $.getJSON('/users').then(function(users) { res = users; console.log(users); }); // <- this sets all users to res for later requests
// $.getJSON('/users/' + res.users[0].id).then(function(user) { console.log(user); });
// $.post('/users', { users: { first: 'Donatas', last: 'Kairys' } }, null, 'json').then(function(user) { console.log(user); });
// $.ajax('/users/' + res.users[0].id, { type: 'put', data: { users: { first: 'Joseph' } }, dataType: 'json'}).then(function(user) { console.log(user); });
// $.ajax('/users/' + res.users[0].id, { type: 'delete', dataType: 'json'}).then(function(res) { console.log(res); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment