Skip to content

Commit 6973a0b

Browse files
committed
Merge pull request #2 from ifwe/cache-requests
Adds basic support for caching GET requests
2 parents fd6fbd6 + 687e16a commit 6973a0b

File tree

16 files changed

+784
-55
lines changed

16 files changed

+784
-55
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Core
55
----
66
- [x] Support "props" options
77
- [ ] Support "pluck" option
8-
- [ ] Add caching layer
8+
- [x] Add caching layer
99
- [ ] Auto-batch requests
1010

1111
Angular Adapter

demo/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ api.route('/users/:userId', {
3434
var userId = req.getParam('userId');
3535
if (userId > 0 && userId < 100) {
3636
// within range of acceptible user ids
37-
return resolve({
37+
return resolve(new Monocle.Resource('/users/' + userId, {
3838
userId: userId,
3939
displayName: 'FPO Display Name ' + userId,
4040
age: 27
41-
});
41+
}, 60000));
4242
}
4343

4444
reject('Invalid user id');
45-
}, 200);
45+
}, 500);
4646
});
4747
}
4848
});

demo/public/angular.html

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,25 @@
9090
<button type="submit">Fetch!</button>
9191
</div>
9292
</form>
93+
94+
<div class="error box" ng-if="demo.error">
95+
<h2>Error</h2>
96+
<pre>{{ demo.error | json:2 }}</pre>
97+
</div>
98+
9399
<div class="user box" ng-if="demo.user">
94-
<h1>User</h1>
100+
<h2>User</h2>
95101
<pre>{{ demo.user | json:2 }}</pre>
96102
</div>
97-
<div class="error box" ng-if="demo.error">
98-
<h1>Error</h1>
99-
<pre>{{ demo.error | json:2 }}</pre>
103+
104+
<div class="cached box">
105+
<h2>Cache</h2>
106+
<ul>
107+
<li ng-repeat="(key, cache) in demo.cached">
108+
<h3 ng-click="shown = !shown">{{ key }}</h3>
109+
<pre ng-show="shown">{{ cache | json:2 }}</pre>
110+
</li>
111+
</ul>
100112
</div>
101113
</body>
102114

@@ -134,26 +146,36 @@ <h1>Error</h1>
134146
.filter(function(prop) {
135147
return this.properties[prop];
136148
}.bind(this));
149+
137150
return monocle.get('/users/' + id, {
138151
props: props
139-
});
152+
})
153+
.finally(function() {
154+
// Expose the cached items for demo purposes
155+
this.cached = monocle.getCache().getAll();
156+
}.bind(this));
140157
};
141158
});
142159

143160
app.controller('DemoCtrl', function(Users, $scope) {
144161
this.Users = Users;
145162
this.userId = 1;
163+
146164
this.fetch = function() {
147165
this.error = null;
148166
this.fetching = true;
149-
Users.get(this.userId).then(function(user) {
167+
168+
Users.get(this.userId)
169+
.then(function(user) {
150170
this.user = user;
151-
}.bind(this)).catch(function(error) {
171+
}.bind(this))
172+
.catch(function(error) {
152173
this.user = null;
153174
this.error = error;
154175
}.bind(this))
155176
.finally(function() {
156177
this.fetching = false;
178+
this.cached = Users.cached;
157179
}.bind(this));
158180
};
159181
});

0 commit comments

Comments
 (0)