-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Could/should isPristine be promise/proxy promise aware? #561
Comments
@richbrown Is it possible to await the fieldType on the |
@snewcomer thank you for pointing me in the right direction so kindly. I have done that and with a few little other tweaks, mainly |
@richbrown No wasting time here! I always learn something new talking with people like you.
Could you detail this more in depth? What is the problem and the approximate solution? We might be able to improve something in this area. |
@snewcomer thank you and your response is timely as it happens as I spoke out of turn and my solution doesn't work in all contexts. So without confusing the issue too much I have included my original Changeset below. The basic premise of the problem is largely as @sandstorm described in issue #551. I am working on a nested interface for Therefore, in this instance, as you can hopefully see below, I am overriding the I have tried import { EmberChangeset, Changeset } from 'ember-changeset';
import { task } from 'ember-concurrency-decorators';
import { notifyPropertyChange } from '@ember/object';
export default class ModuleTemplateChangeset extends EmberChangeset {
_fieldTemplates = [];
_fieldTemplateChangesetKeys = ['name', 'fieldType', 'defaultValue', 'listOptions'];
get fieldTemplates() {
return this._fieldTemplates.rejectBy('isDeleted');
}
get isDirty() {
return !this.isPristine;
}
get isPristine() {
return super.isPristine &&
this.areFieldTemplatesPristine;
}
get areFieldTemplatesPristine() {
return this._fieldTemplates.isEvery('isPristine') &&
!this._fieldTemplates.isAny('isNew') &&
!this._fieldTemplates.isAny('isDeleted')
}
constructor() {
super(...arguments);
this._buildFieldTemplateChangesets();
}
addFieldTemplate(fieldTemplate) {
const changeset = Changeset(fieldTemplate, null, null, { changesetKeys: this._fieldTemplateChangesetKeys });
this._fieldTemplates.pushObject(changeset);
this._notifyFieldTemplateChanges();
}
removeFieldTemplate(fieldTemplateChangeset) {
// I don't think that ember-changeset supports deletion so
// we need to go to the underlying object to propagate the
// change
fieldTemplateChangeset.data.deleteRecord();
this._notifyFieldTemplateChanges();
}
@task
*save() {
let fieldTemplatesToSave =
this._fieldTemplates.filter(fieldTemplate => {
return fieldTemplate.isDirty || fieldTemplate.isNew || fieldTemplate.isDeleted;
});
let fieldTemplatePromises = fieldTemplatesToSave.map((fieldTemplate) => fieldTemplate.save());
try {
let result = yield Promise.all([super.save(...arguments), fieldTemplatePromises]);
let deletedFieldTemplates = fieldTemplatesToSave.filterBy('isDeleted');
this._fieldTemplates.removeObjects(deletedFieldTemplates);
this._notifyFieldTemplateChanges();
return result;
}
catch (e) {
console.log(e);
}
}
rollback() {
super.rollback(...arguments);
let newFieldTemplates = [];
let deletedFieldTemplates = [];
this._fieldTemplates.forEach((fieldTemplate) => {
if (fieldTemplate.isNew) {
newFieldTemplates.pushObject(fieldTemplate);
}
else if (fieldTemplate.isDeleted) {
deletedFieldTemplates.pushObject(fieldTemplate);
}
else if (fieldTemplate.isDirty) {
fieldTemplate.rollback();
}
})
this._fieldTemplates.removeObjects(newFieldTemplates);
// I don't think that ember-changeset supports deletion so
// we need to go to the underlying object to propagate the
// change
deletedFieldTemplates.forEach((fieldTemplate) => fieldTemplate.data.rollbackAttributes());
}
_buildFieldTemplateChangesets() {
this._fieldTemplates = this.data.fieldTemplates.map(fieldTemplate => {
return Changeset(fieldTemplate, null, null, { changesetKeys: this._fieldTemplateChangesetKeys });
});
}
_notifyFieldTemplateChanges() {
notifyPropertyChange(this, 'fieldTemplates');
notifyPropertyChange(this, 'isDirty');
notifyPropertyChange(this, 'isPristine');
}
} |
I am not really sure if this is a bug, a feature request or just my misunderstanding of the intended design.
I have two Ember Data models that are related to each other:
The issue I am experiencing occurs when I use a changeset in an hbs form and change the
fieldType
value, it makes the changeset dirty. However, when I change it back to the originalfieldType
the changeset is still dirty, as in the following pseudo code:I believe this is because of the comparison in validated-changeset BufferedChangeset#_setProperty where
oldValue !== value
. TheoldValue
is a proxy to the original promise and the new value is a resolved model that has been set on the changeset. So in this example theoldValue.content === value //is true
.So my question is should this comparison be promise aware, or I am misusing the changeset?
I have worked around this by making the relationship
@belongsTo('field-type', {async: false}) fieldType
and that works fine, but I suspect other aspects of the app I am working on may need similar functionality but I would prefer not to make the relationshipasync: false
The text was updated successfully, but these errors were encountered: