I have two Ember models which define a relationship in which a license can belong to a customer, and a customer can have many licenses:
models/license.js
export default DS.Model.extend({
code: DS.attr('string'),
customer: DS.belongsTo('customer'),
});
models/customer.js
export default DS.Model.extend({
license: DS.hasMany('license'),
});
In my route I have a function that sets which customer a license belongs to. I have to fetch both the relevant customer and license records from Ember data, so I use nested findRecord calls.
routes/license.js
updateLicense: function(license) {
var self = this;
self.get('store').findRecord('license', license.id).then(function(licenseRecord) {
self.get('store').findRecord('customer', 11).then(function(customer) {
licenseRecord.set('customer', customer);
licenseRecord.set('code', '23412342');
});
});
}
The first time the updateLicense action runs, the customer is correctly set for the relevant license record. The HBS below correctly shows the customer ID where exists.
<td class="customer"><button >Update</button></td>
However, each time the action runs after that, the customer is set and then immediately unset, hence the ID appears for an instant and then disappears. The data tab in Ember inspector confirms that the customer relationship for these licenses has a value of null for data.
The line licenseRecord.set('code', '23412342');
in the updateLicense action always works correctly. Why would Ember data be setting and then unsetting the customer for all but the first license?
Aucun commentaire:
Enregistrer un commentaire