简体   繁体   中英

Create new record and commit transaction with ember-data

I am trying to trigger an adequate PUT request when I add a new record to a one to many field using ember-data.

var track = genre.get('tracks').createRecord({name: "Hit me baby one more time!"})
track.get('transaction').commit()

This will send a PUT request to '/genres' and send all the genre attributes and other tracks as well.

// PUT /genres
{"genre":{"name":"Pop","tracks": [{"name":"Hit me baby one more time!","genre_id":null}]}}

But I would prefer sth. like:

// PUT /tracks
{"track": {"name":"Hit me baby one more time!", "genre_id":1}}}

If somehow possible I would also like to add an additional parameter automatically for PUT requests on /tracks. These are my models:

App.Genre = DS.Model.extend({
  name:         DS.attr('string'),
  tracks:       DS.hasMany('App.Track')
})

App.Track = DS.Model.extend({
  genre:      DS.belongsTo('App.Genre'),
  uri:        DS.attr('string')
})

My bad, in the REST adapter I had this setting:

DS.RESTAdapter.map('App.Genre', {
  tracks: { embedded: 'always' }
})

Setting embedded to load solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM