consider following 2 models: car , owner
i have car record current owner (bob jones) , have owner record (bob jones) details... address, phone etc.
in car form (car model), have this:
export default model.extend({ "owner_id": ds.belongsto('owner'), "year": attr(''), "model": attr(''), "make": attr('') })
stored on db end, id '12345', corresponding bob jones owner record.
when load car record (2015 jaguar), connect bob jones record tells me phone, address etc. example, if print out
{{owner_id.id}} - {{owner_id.name}}
i show:
12345 - bob jones
my question is, happens if change owners of car. if select dropdown list
{{my-select name="owner_id" content=ownerlist selection=owner_id.id prompt="select owner" required=true }}
from component:
import ember 'ember'; export default ember.component.extend({ content: [], prompt: null, optionvaluepath: 'value', optionlabelpath: 'label', init: function () { this._super(...arguments); if (!this.get('content')) { this.set('content', []); } }, actions: { change: function () { let selectedindex = this.$('select')[0].selectedindex; let content = this.get('content'); // decrement index 1 if have prompt let hasprompt = !!this.get('prompt'); let contentindex = hasprompt ? selectedindex - 1 : selectedindex; let _selection = content[contentindex]; this.sendaction('willchangeaction', _selection); if (this.get('optionvaluepath')) { this.set('selection', _selection[this.get('optionvaluepath')]); } else { this.set('selection', _selection); } this.sendaction('didchangeaction', _selection); } } });
and change value... error:
a record's id cannot changed once in loaded state
how go updating 2015 jaguar car record change owner, , load in details of new owner?
you're changing id of owner instead of changing owner_id
prop on jaguar. changed owner_id
prop owner
clarify belongsto holds.
the general strategy i'm taking select owner of car, pass controller save car.
models/car.js
export default model.extend({ "owner": ds.belongsto('owner'), // ... })
routes/cars/car.js
import ember 'ember'; export default ember.route.extend({ model(params) { return ember.rsvp.hash({ car: this.store.find('car', params.id), ownerlist: // owner list }) } })
controllers/cars/car.js
import ember 'ember'; export default ember.controller.extend({ actions{ ownerchanged(newowner) { // if select setup properly, newowner should instance of owner. const car = this.get('model.car'); car.set('owner', newowner); car.save } } })
templates/cars/car.hbs.
{{my-select name="owner" content=model.ownerlist selection=model.car.owner.id prompt="select owner" required=true didchangeaction='ownerchanged' }}
the code in route's model hook there clarity, if you're taking different approach, adjust names accordingly
Comments
Post a Comment