-
-
Save robustdj/f92d108943ba667206d2 to your computer and use it in GitHub Desktop.
Ember store.js file to support creating a model with its associations
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
App.store = DS.store.create | |
revision: 4 | |
adapter: DS.RESTAdapter.create | |
bulkCommit: true | |
# Override DS.RESTAdapter#createRecords to send data to the server in the format that we | |
# need. By default, DS.RESTAdapter will format the data such that it pluralizes the entity | |
# name. However, our rails controllers (for creating) expect a POST with something like: | |
# | |
# { | |
# post: { | |
# title: "some title" | |
# content: "blah blah" | |
# categorizations: [ | |
# { | |
# category_id: 2 | |
# } | |
# ] | |
# } | |
# } | |
createRecords: (store, type, records) -> | |
return @_super(store, type, records) if @get('bulkCommit') == false | |
root = @rootForType(type) | |
plural = @pluralize(root) | |
data = {} | |
if records.get('length') > 1 | |
data[plural] = records.map (record) -> | |
record.toJSON() | |
else if records.get('length') == 1 | |
data[root] = records.get('firstObject').toJSON() | |
@ajax(@buildURL(root), "POST", | |
data: data | |
context: this | |
success: (json) -> | |
if records.get('length') > 1 | |
@didCreateRecords(store, type, records, json) | |
else | |
@didCreateRecord(store, type, records.get('firstObject'), json) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment