ducky/web

Paddy 2015-07-07

21:bc1478742a50 Go to Latest

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?

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