Skip to content

Commit

Permalink
Adding examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mzabriskie committed Feb 3, 2015
1 parent e880940 commit 7efc095
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 82 deletions.
82 changes: 0 additions & 82 deletions example/index.html

This file was deleted.

9 changes: 9 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# axios examples

To run the examples:

1. Clone this repo
2. Run `npm install`
3. Run `grunt build`
4. Run `http-server -p 3000`
5. Open http://localhost:3000/examples
44 changes: 44 additions & 0 deletions examples/all/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!doctype html>
<html>
<head>
<title>axios - all example</title>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
</head>
<body class="container">
<h1>axios.all</h1>

<div>
<h3>User</h3>
<div class="row">
<img id="useravatar" src="" class="col-md-1"/>
<div class="col-md-3">
<strong id="username"></strong>
</div>
</div>
<hr/>
<h3>Orgs</h3>
<ul id="orgs" class="list-unstyled"></ul>
</div>

<script src="/dist/axios.min.js"></script>
<script>
axios.all([
axios.get('https://api.github.com/users/mzabriskie'),
axios.get('https://api.github.com/users/mzabriskie/orgs')
]).then(axios.spread(function (user, orgs) {
document.getElementById('useravatar').src = user.data.avatar_url;
document.getElementById('username').innerHTML = user.data.name;
document.getElementById('orgs').innerHTML = orgs.data.map(function (org) {
return (
'<li class="row">' +
'<img src="' + org.avatar_url + '" class="col-md-1"/>' +
'<div class="col-md-3">' +
'<strong>' + org.login + '</strong>' +
'</div>' +
'</li>'
);
}).join('');
}));
</script>
</body>
</html>
33 changes: 33 additions & 0 deletions examples/get/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!doctype html>
<html>
<head>
<title>axios - get example</title>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
</head>
<body class="container">
<h1>axios.get</h1>
<ul id="people" class="list-unstyled"></ul>

<script src="/dist/axios.min.js"></script>
<script>
axios.get('people.json')
.then(function (response) {
document.getElementById('people').innerHTML = response.data.map(function (person) {
return (
'<li class="row">' +
'<img src="https://avatars.githubusercontent.com/u/' + person.avatar + '?s=50" class="col-md-1"/>' +
'<div class="col-md-3">' +
'<strong>' + person.name + '</strong>' +
'<div>Github: <a href="https://github.com/' + person.github + '" target="_blank">' + person.github + '</a></div>' +
'<div>Twitter: <a href="https://twitter.com/' + person.twitter + '" target="_blank">' + person.twitter + '</a></div>' +
'</div>' +
'</li><br/>'
);
}).join('');
})
.catch(function(data) {
document.getElementById('people').innerHTML = '<li class="text-danger">' + data + '</li>';
});
</script>
</body>
</html>
File renamed without changes.
45 changes: 45 additions & 0 deletions examples/post/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!doctype html>
<html>
<head>
<title>axios - post example</title>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
</head>
<body class="container">
<h1>axios.post</h1>

<form role="form" class="form" onsubmit="return false;">
<div class="form-group">
<label for="url">URL</label>
<input id="url" type="text" class="form-control"/>
</div>
<div class="form-group">
<label for="data">Body</label>
<textarea id="data" class="form-control" rows="5"></textarea>
</div>
<button id="post" type="button" class="btn btn-primary">POST</button>
</form>

<div id="output" class="container"></div>

<script src="/dist/axios.min.js"></script>
<script>
(function () {
var output = document.getElementById('output');
document.getElementById('post').onclick = function () {
var url = document.getElementById('url').value;
var data = document.getElementById('data').value;

axios.post(url, JSON.parse(data))
.then(function (res) {
output.className = 'container';
output.innerHTML = res.data;
})
.catch(function (res) {
output.className = 'container text-danger';
output.innerHTML = res.data;
});
};
})();
</script>
</body>
</html>
44 changes: 44 additions & 0 deletions examples/transform-response/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!doctype html>
<html>
<head>
<title>axios - transform response example</title>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
</head>
<body class="container">
<h1>transformResponse</h1>

<div class="row">
<img id="useravatar" src="" class="col-md-1"/>
<div class="col-md-3">
<strong id="username"></strong><br/>
Created: <span id="created"></span><br/>
Updated: <span id="updated"></span>
</div>
</div>

<script src="/dist/axios.min.js"></script>
<script>
var ISO_8601 = /(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})Z/;
function formatDate(d) {
return (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear();
}

axios.get('https://api.github.com/users/mzabriskie', {
transformResponse: axios.defaults.transformResponse.concat(function (data, headers) {
Object.keys(data).forEach(function (k) {
if (ISO_8601.test(data[k])) {
data[k] = new Date(Date.parse(data[k]));
}
});
return data;
})
})
.then(function (res) {
document.getElementById('useravatar').src = res.data.avatar_url;
document.getElementById('username').innerHTML = res.data.name;
document.getElementById('created').innerHTML = formatDate(res.data.created_at);
document.getElementById('updated').innerHTML = formatDate(res.data.updated_at);
});
</script>
</body>
</html>

0 comments on commit 7efc095

Please sign in to comment.