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.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/models/subscriptions.js Tue Jul 07 21:29:19 2015 -0400 1.3 @@ -0,0 +1,46 @@ 1.4 +import Collection from 'ampersand-rest-collection' 1.5 +import Subscription from './subscription' 1.6 +import config from '../config' 1.7 +import refresh from '../helpers/oauth-refresh' 1.8 +import app from 'ampersand-app' 1.9 +import isObject from 'lodash.isobject' 1.10 + 1.11 +export default Collection.extend({ 1.12 + model: Subscription, 1.13 + url: config.urlBase + '/subscriptions/subscriptions', 1.14 + ajaxConfig () { 1.15 + return { 1.16 + headers: { 1.17 + 'Authorization': 'Bearer '+app.me.access_token, 1.18 + 'Accept': 'application/json', 1.19 + } 1.20 + } 1.21 + }, 1.22 + 1.23 + sync: refresh.Sync, 1.24 + 1.25 + create (params) { 1.26 + let options = { 1.27 + data: JSON.stringify({ 1.28 + 'user_id': params.user_id, 1.29 + 'email': params.email, 1.30 + 'stripe_token': params.stripe_token, 1.31 + 'plan': params.plan, 1.32 + }), 1.33 + } 1.34 + let moc = this 1.35 + options.success = function(resp) { 1.36 + let serverAttrs = moc.parse(resp, options) 1.37 + if (options.wait) serverAttrs = assign({}, serverAttrs) 1.38 + if (isObject(serverAttrs) && !moc.set(serverAttrs, options)) { 1.39 + // TODO: throw error? 1.40 + return 1.41 + } 1.42 + moc.trigger('sync', moc, resp, options) 1.43 + } 1.44 + options.error = function(resp) { 1.45 + moc.trigger('error', moc, resp, options) 1.46 + } 1.47 + refresh.Sync('create', moc, options) 1.48 + } 1.49 +})