Skip to content

Commit

Permalink
Merge pull request #2 from ifwe/cache-requests
Browse files Browse the repository at this point in the history
Adds basic support for caching GET requests
  • Loading branch information
djvirgen committed Oct 1, 2015
2 parents fd6fbd6 + 687e16a commit 6973a0b
Show file tree
Hide file tree
Showing 16 changed files with 784 additions and 55 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Core
----
- [x] Support "props" options
- [ ] Support "pluck" option
- [ ] Add caching layer
- [x] Add caching layer
- [ ] Auto-batch requests

Angular Adapter
Expand Down
6 changes: 3 additions & 3 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ api.route('/users/:userId', {
var userId = req.getParam('userId');
if (userId > 0 && userId < 100) {
// within range of acceptible user ids
return resolve({
return resolve(new Monocle.Resource('/users/' + userId, {
userId: userId,
displayName: 'FPO Display Name ' + userId,
age: 27
});
}, 60000));
}

reject('Invalid user id');
}, 200);
}, 500);
});
}
});
Expand Down
36 changes: 29 additions & 7 deletions demo/public/angular.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,25 @@
<button type="submit">Fetch!</button>
</div>
</form>

<div class="error box" ng-if="demo.error">
<h2>Error</h2>
<pre>{{ demo.error | json:2 }}</pre>
</div>

<div class="user box" ng-if="demo.user">
<h1>User</h1>
<h2>User</h2>
<pre>{{ demo.user | json:2 }}</pre>
</div>
<div class="error box" ng-if="demo.error">
<h1>Error</h1>
<pre>{{ demo.error | json:2 }}</pre>

<div class="cached box">
<h2>Cache</h2>
<ul>
<li ng-repeat="(key, cache) in demo.cached">
<h3 ng-click="shown = !shown">{{ key }}</h3>
<pre ng-show="shown">{{ cache | json:2 }}</pre>
</li>
</ul>
</div>
</body>

Expand Down Expand Up @@ -134,26 +146,36 @@ <h1>Error</h1>
.filter(function(prop) {
return this.properties[prop];
}.bind(this));

return monocle.get('/users/' + id, {
props: props
});
})
.finally(function() {
// Expose the cached items for demo purposes
this.cached = monocle.getCache().getAll();
}.bind(this));
};
});

app.controller('DemoCtrl', function(Users, $scope) {
this.Users = Users;
this.userId = 1;

this.fetch = function() {
this.error = null;
this.fetching = true;
Users.get(this.userId).then(function(user) {

Users.get(this.userId)
.then(function(user) {
this.user = user;
}.bind(this)).catch(function(error) {
}.bind(this))
.catch(function(error) {
this.user = null;
this.error = error;
}.bind(this))
.finally(function() {
this.fetching = false;
this.cached = Users.cached;
}.bind(this));
};
});
Expand Down
Loading

0 comments on commit 6973a0b

Please sign in to comment.