Created
May 2, 2014 04:22
-
-
Save brianpattison/89256db1f42493a28bb3 to your computer and use it in GitHub Desktop.
Spree + Ember-Data is coming...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backend.ApplicationSerializer = DS.RESTSerializer.extend({ | |
extractMeta: function(store, type, payload) { | |
store.metaForType(type, { | |
count: payload.count, | |
currentPage: payload.current_page, | |
pages: payload.pages | |
}); | |
delete payload.count; | |
delete payload.current_page; | |
delete payload.pages; | |
}, | |
keyForAttribute: function(attr) { | |
return Ember.String.underscore(attr); | |
}, | |
keyForRelationship: function(key, relationship) { | |
key = Ember.String.singularize(key); | |
if (relationship === 'hasMany') { | |
return Ember.String.underscore(key) + '_ids'; | |
} else { | |
return Ember.String.underscore(key) + '_id'; | |
} | |
}, | |
serializeIntoHash: function(hash, type, record, options) { | |
var root = Ember.String.underscore(type.typeKey); | |
hash[root] = this.serialize(record, options); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backend.Order = DS.Model.extend({ | |
// --------------------------------------------------------- | |
// Relationships | |
// --------------------------------------------------------- | |
billAddress: DS.belongsTo('address'), | |
shipAddress: DS.belongsTo('address'), | |
// --------------------------------------------------------- | |
// Attributes | |
// --------------------------------------------------------- | |
number: DS.attr('string'), | |
itemTotal: DS.attr('number'), | |
state: DS.attr('string'), | |
total: DS.attr('number') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backend.OrderSerializer = Backend.ApplicationSerializer.extend({ | |
extractSingle: function(store, type, payload, id, requestType) { | |
var order = payload, | |
billAddress = payload.bill_address, | |
shipAddress = payload.ship_address, | |
lineItems = payload.line_items, | |
payments = payload.payments, | |
shipments = payload.shipments, | |
adjustments = payload.adjustments; | |
delete order.bill_address; | |
delete order.ship_address; | |
delete order.line_items; | |
delete order.payments; | |
delete order.shipments; | |
delete order.adjustments; | |
order.bill_address_id = billAddress.id; | |
order.ship_address_id = shipAddress.id; | |
order.line_item_ids = lineItems.mapBy('id'); | |
order.payment_ids = payments.mapBy('id'); | |
order.shipment_ids = shipments.mapBy('id'); | |
order.adjustment_ids = adjustments.mapBy('id'); | |
payload = { | |
order: order, | |
addresses: [billAddress, shipAddress], | |
line_items: lineItems | |
// payments: payments, | |
// shipments: shipments, | |
// adjustments: adjustments | |
}; | |
return this._super(store, type, payload, id, requestType); | |
}, | |
normalizeId: function(hash) { | |
hash.id = hash.number; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment