ducky/web
2015-07-07
ducky/web/src/models/subscriptions.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 Collection from 'ampersand-rest-collection'
2 import Subscription from './subscription'
3 import config from '../config'
4 import refresh from '../helpers/oauth-refresh'
5 import app from 'ampersand-app'
6 import isObject from 'lodash.isobject'
8 export default Collection.extend({
9 model: Subscription,
10 url: config.urlBase + '/subscriptions/subscriptions',
11 ajaxConfig () {
12 return {
13 headers: {
14 'Authorization': 'Bearer '+app.me.access_token,
15 'Accept': 'application/json',
16 }
17 }
18 },
20 sync: refresh.Sync,
22 create (params) {
23 let options = {
24 data: JSON.stringify({
25 'user_id': params.user_id,
26 'email': params.email,
27 'stripe_token': params.stripe_token,
28 'plan': params.plan,
29 }),
30 }
31 let moc = this
32 options.success = function(resp) {
33 let serverAttrs = moc.parse(resp, options)
34 if (options.wait) serverAttrs = assign({}, serverAttrs)
35 if (isObject(serverAttrs) && !moc.set(serverAttrs, options)) {
36 // TODO: throw error?
37 return
38 }
39 moc.trigger('sync', moc, resp, options)
40 }
41 options.error = function(resp) {
42 moc.trigger('error', moc, resp, options)
43 }
44 refresh.Sync('create', moc, options)
45 }
46 })