Skip to content

Instantly share code, notes, and snippets.

@robustdj
Created October 13, 2012 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robustdj/f92d108943ba667206d2 to your computer and use it in GitHub Desktop.
Save robustdj/f92d108943ba667206d2 to your computer and use it in GitHub Desktop.
Ember store.js file to support creating a model with its associations
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