You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After an object has been saved with a child, at some point I want to remove that child. I would assume model.unset should do the job, but it results in an error (see below).
I have tried various strategies, none of which were succesful. What is the proper way to delete/remove a child item from a state object?
These were my intents, and the error message/code location where it failed:
Starting with the one I assumed would work (but apparently only works for actual properties and not children):
model.unset("relatedSystem");
model.save();
Results in TypeError: def is undefined at the if (def.required) line in the unset function, since this._definition only hold the real properties and not the children (is that on purpose?)
unset: function (attrs, options) {
attrs = Array.isArray(attrs) ? attrs : [attrs];
forEach(attrs, function (key) {
var def = this._definition[key];
var val;
if (def.required) {
Then trying to replicate what the unset function does when a property is not required (but without using the assign part because I was too lazy to figure out how to require lodash properly):
var val;
model.set("relatedSystem", val);
model.save();
That one doens't log any error, but going to that object's page (and properly refreshing) the child is still defined on the object.
Just trying to delete the object's property:
delete model.relatedSystem;
model.save();
Results in TypeError: this[key] is undefined at the third line in the serialize function:
serialize: function () {
var res = this.getAttributes({props: true}, true);
forEach(this._children, function (value, key) {
res[key] = this[key].serialize();
}, this);
Sorry for the late response. You cannot unset a child as it's not a property, but an instance that belongs to the parent.
You could try using a state property type instead (which you could unset).
Another solution is to do: model.set('relatedSystem', {})
and you could also try: model.relatedSystem.unset()
If nothing works, could you add a failing test as a PR so that we may work a solution out? I feel that allowing to unset or empty a child should be allowed someway.
Assume an object's model defined as such:
After an object has been saved with a child, at some point I want to remove that child. I would assume
model.unset
should do the job, but it results in an error (see below).I have tried various strategies, none of which were succesful. What is the proper way to delete/remove a child item from a state object?
These were my intents, and the error message/code location where it failed:
Starting with the one I assumed would work (but apparently only works for actual properties and not children):
Results in
TypeError: def is undefined
at theif (def.required)
line in theunset
function, sincethis._definition
only hold the real properties and not the children (is that on purpose?)Then trying to replicate what the
unset
function does when a property is not required (but without using theassign
part because I was too lazy to figure out how torequire
lodash properly):That one doens't log any error, but going to that object's page (and properly refreshing) the child is still defined on the object.
Just trying to delete the object's property:
Results in
TypeError: this[key] is undefined
at the third line in theserialize
function:Another intent:
Results in
TypeError: this[key].serialize is not a function
on the same line as the previous error.Last intent:
Results in the same error as the previous intent.
The text was updated successfully, but these errors were encountered: