Skip to content

Commit c89591a

Browse files
committed
fix: when using objects as prefill, the next prefilled value would return the reference of the one before
1 parent 0bf2176 commit c89591a

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<h2>Medium Editor</h2>
22
<ec-medium-editor [(ngModel)]="html" [options]="options"></ec-medium-editor>
33
<pre><code>{{html}}</code></pre>
4-
<a class="btn" (click)="html='<h1>Test</h1>'">Change Model from outside</a>
4+
<a class="btn" (click)="html='<h1>Test</h1>'">Change Model from outside</a>

packages/core/src/form/form.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export class Form<Object> extends Item<Object> {
1919
this.fields = [];
2020
if (this.config.fields) {
2121
Object.keys(this.config.fields)
22-
.forEach((property) => {
23-
this.fields.push(new Field(property, this.config.fields[property]));
24-
});
22+
.forEach((property) => {
23+
this.fields.push(new Field(property, this.config.fields[property]));
24+
});
2525
} else {
2626
this.getProperties().forEach((property) => {
2727
this.fields.push(new Field(property, { type: typeof this.resolve(property) }));
@@ -37,10 +37,16 @@ export class Form<Object> extends Item<Object> {
3737
/** Returns the original value of the property, if any. */
3838
getValue(property: string) {
3939
if (!this.body && this.config.fields && this.config.fields[property]) {
40+
// If the prefill is not a primitive, return a clone to stay pristine
41+
if (Array.isArray(this.config.fields[property].prefill)) {
42+
return this.config.fields[property].prefill.slice(0);
43+
} else if (typeof this.config.fields[property].prefill === 'object') {
44+
return Object.assign({}, this.config.fields[property].prefill);
45+
}
4046
// if no body is present, the prefills are used
4147
return this.config.fields[property].prefill;
4248
} else {
4349
return this.resolve(property);
4450
}
4551
}
46-
}
52+
}

packages/data/src/crud/crud.service.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class CrudService {
3535
/** Gives true if the given change fits all property values of the filter. */
3636
matches(change: Update, filter: Update): boolean {
3737
return Object.keys(filter)
38-
.reduce((match, key) => match && change[key] === filter[key], true);
38+
.reduce((match, key) => match && change[key] === filter[key], true);
3939
}
4040

4141
/** Yields an observable that emits for all updates that match the given filter */
@@ -52,11 +52,11 @@ export class CrudService {
5252
return this.update(model, entry, value);
5353
}
5454
return this.create(model, value)
55-
.then((_entry) => {
56-
return _entry;
57-
}).catch((err) => {
58-
return Promise.reject(err);
59-
});
55+
.then((_entry) => {
56+
return _entry;
57+
}).catch((err) => {
58+
return Promise.reject(err);
59+
});
6060
}
6161

6262
/** Updates the given entry with the new value. Fires the "update" change. */
@@ -68,10 +68,10 @@ export class CrudService {
6868
this.changes.next({ model, entry: _entry, type: 'put' });
6969
return _entry;
7070
})
71-
.catch((err) => {
72-
Object.assign(entry, this.clean(oldValues)); // fall back to old values
73-
return Promise.reject(err);
74-
});
71+
.catch((err) => {
72+
Object.assign(entry, this.clean(oldValues)); // fall back to old values
73+
return Promise.reject(err);
74+
});
7575
}
7676

7777
/** Returns true if the given field key is an immutable system property */
@@ -83,11 +83,13 @@ export class CrudService {
8383
/** Removes all null or undefined values from the given object */
8484
clean(value: Object): Object {
8585
for (const key in value) {
86-
if (value[key] === '') { // clear empty strings
87-
value[key] = null;
88-
}
89-
if (this.isImmutableProperty(key)) { // filter system properties
90-
delete value[key];
86+
if (value.hasOwnProperty(key)) {
87+
if (value[key] === '') { // clear empty strings
88+
value[key] = null;
89+
}
90+
if (this.isImmutableProperty(key)) { // filter system properties
91+
delete value[key];
92+
}
9193
}
9294
}
9395
return value;
@@ -96,14 +98,14 @@ export class CrudService {
9698
/** Creates a new entry with the given value for the given model. Fires the "create" change. */
9799
create(model: string, value: Object): Promise<EntryResource> {
98100
return this.sdk.api.createEntry(model, this.clean(value))
99-
.then((entry) => {
100-
// console.log('created entry', entry);
101-
// TODO make sure leveled entries are returned leveled
102-
this.changes.next({ model, entry, type: 'post' });
103-
return entry;
104-
}).catch((err) => {
105-
return Promise.reject(err);
106-
});
101+
.then((entry) => {
102+
// console.log('created entry', entry);
103+
// TODO make sure leveled entries are returned leveled
104+
this.changes.next({ model, entry, type: 'post' });
105+
return entry;
106+
}).catch((err) => {
107+
return Promise.reject(err);
108+
});
107109
}
108110

109111
/** deletes the given entry and emits the "delete" change. */

0 commit comments

Comments
 (0)