ducky/web
2015-07-07
Parent:791aec3eb17b
ducky/web/src/models/profile.js
Implement subscriptions. Create a Subscription model and a Subscriptions collection, and attach them to the app context. Add a helper to our Profile model to retrieve the Subscription of that model. Still not sure this should be on the Profile--wouldn't it be better on the Me model? Isn't that generally where we would need it?
1 import Model from 'ampersand-model'
2 import refresh from '../helpers/oauth-refresh'
4 export default Model.extend({
5 props: {
6 'id': 'string',
7 'name': 'string',
8 'created': 'date',
9 'last_seen': 'date',
10 },
11 sync: refresh.Sync,
12 parse (attrs) {
13 if (attrs.profiles && attrs.profiles.length == 1) {
14 attrs = attrs.profiles[0]
15 }
16 return attrs
17 },
18 ajaxConfig () {
19 return {
20 headers: {
21 'Authorization': 'Bearer '+app.me.access_token,
22 'Accept': 'application/json',
23 }
24 }
25 },
27 subscription() {
28 return new Promise((resolve, reject) => {
29 app.subscriptions.getOrFetch(this.id, (err, model) => {
30 if (err) {
31 reject(err)
32 } else {
33 resolve(model)
34 }
35 })
36 })
37 },
38 })