Skip to content
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

Not possible to delete a child state object #179

Open
e2jk opened this issue Jul 9, 2015 · 4 comments
Open

Not possible to delete a child state object #179

e2jk opened this issue Jul 9, 2015 · 4 comments

Comments

@e2jk
Copy link

e2jk commented Jul 9, 2015

Assume an object's model defined as such:

module.exports = AmpersandModel.extend({
    props: {
        id: 'any',
        [snip]
    },
    children: {
      relatedSystem: System
    },

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);

Another intent:

model.relatedSystem = {"systemName":"","vendorName":"","contactName":"","contactPhone":"","contactEmail":""};
model.save();

Results in TypeError: this[key].serialize is not a function on the same line as the previous error.

Last intent:

model.relatedSystem = {};
model.save();

Results in the same error as the previous intent.

@e2jk
Copy link
Author

e2jk commented Jul 15, 2015

Hi there, any idea about how to remove a child from an object?

@e2jk
Copy link
Author

e2jk commented Sep 21, 2015

Hi there, any thoughts about this issue?

@pgilad
Copy link
Member

pgilad commented Sep 21, 2015

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.

@pgilad
Copy link
Member

pgilad commented Oct 19, 2015

@e2jk Did my comment help you solve your problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants