Created
August 11, 2014 16:48
-
-
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.
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
// 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