We all know that we have get
and set
in ember, but how do you delete
a key in a emberjs object?
Assume an object looks like this
Ember.Object.create({
conf: {
name: 'John',
age: 16
}
});
During a data transport I would need to delete a key from conf
, let's say age
. How do you do that to correctly delete a key away from conf
? I have tried set('conf.age', null)
or undefined
but seems like not working at all.
EDIT
A little bit of background. When I say it doesn't work for setting it undefined or null, it means it doesn't suits my need. My classes is automatically saving the conf
data into a Mongo collection. So let's say now you have one key that is unused and you want to get rid of it, how do you remove it?
Take note that there is a bigger class governing the saving process/data validation and so extending the controller just to do the deletion doesn't fit too well (ugly). And there is this beforeSave
action that the extended class can hook on to clean up unused keys, but the problem is just that how to remove this key?
It seems like such a simple action do not exist in ember, probably to deal with all sort of binding/observers..
We all know that we have get
and set
in ember, but how do you delete
a key in a emberjs object?
Assume an object looks like this
Ember.Object.create({
conf: {
name: 'John',
age: 16
}
});
During a data transport I would need to delete a key from conf
, let's say age
. How do you do that to correctly delete a key away from conf
? I have tried set('conf.age', null)
or undefined
but seems like not working at all.
EDIT
A little bit of background. When I say it doesn't work for setting it undefined or null, it means it doesn't suits my need. My classes is automatically saving the conf
data into a Mongo collection. So let's say now you have one key that is unused and you want to get rid of it, how do you remove it?
Take note that there is a bigger class governing the saving process/data validation and so extending the controller just to do the deletion doesn't fit too well (ugly). And there is this beforeSave
action that the extended class can hook on to clean up unused keys, but the problem is just that how to remove this key?
It seems like such a simple action do not exist in ember, probably to deal with all sort of binding/observers..
Share Improve this question edited Mar 14, 2013 at 10:27 Lionel Chan asked Mar 12, 2013 at 8:05 Lionel ChanLionel Chan 8,0695 gold badges42 silver badges70 bronze badges 4- When you say set('conf.age', undefined) not working at all, you mean theObject.get("conf.age") is returning 16 instead of undefined even after setting it to undefined ? – Mudassir Ali Commented Mar 12, 2013 at 10:22
-
There is one reason for this. This
conf
is going to be saved into the db with some generic checking. I was hoping thisconf.age
will not be saved into the db. For now I was setting it to undefined, but it shows up asconf.age: ''
in the db. – Lionel Chan Commented Mar 14, 2013 at 10:18 - Why do you need to delete a key ? Basically when you POST to your server {name: 'John', age: undefined}, you can add a server side validation on params['age'] value if it is undefined do nothing, else modify the age to a newer one, or may be I did not understand your question properly? Or do you mean that you delete a key on the client side for a model and it should delete the respective attribute for the model on the server-side(mongo object) ? – Mudassir Ali Commented Mar 14, 2013 at 10:31
- Nah, I know it can be done on the server side (which is what I am doing it now) to get rid of this key. Just my question is how is it going to be done on client side if I want to get rid of it before submitting. Perhaps my example is not too well formatted and caused confusion. The original code covers over thousands of lines and prises of many inheritance and extends so I thought it would be easy to just make it simple. – Lionel Chan Commented Mar 14, 2013 at 10:54
4 Answers
Reset to default 8if you set a property on an ember object:
this.set('myObject.keyA', [1,2,3]);
it will now keep track of that key
Ember.keys('myObject'); // will show ['keyA'];
you cannot delete a key by setting it to null
this.set('myObject.keyA', null);
since
Ember.keys('myObject'); // will STILL show ['keyA'];
instead simply delete it just as a javascript object
delete this.get('myObject').keyA;
then confirm the key is gone
Ember.keys('myObject'); // will show [] an empty array;
Have you tried to simply 'delete' the key (https://developer.mozilla/en-US/docs/JavaScript/Reference/Operators/delete) ? Assuming that your Ember object is named obj, I would do
delete obj.conf.age;
In fact u can define your own function to handle this like
Ember.Object.create({
conf: {
name: 'John',
age: 16
},
deleteProp:function(key)
{
delete this.conf[key]
}
});
Just adding to @mihai answer:
If you wanna delete nested propery you could do something like this:
function deleteKeyFromPath([object, path]) {
let propertyPathArray = path.split('.');
if (propertyPathArray.length > 1) {
let keyToDelete = propertyPathArray.pop();
let parentPropertyPath = propertyPathArray.join('.');
delete get(object, parentPropertyPath)[keyToDelete];
} else {
delete object[path];
}
}
let user = {
email: '[email protected]',
personalInfo: {
name: 'Jack',
address: {
line1: 'westwish st',
line2: 'washmasher',
city: 'wallas',
state: 'WX'
}
}
}
To delete 'name', call deleteKeyFromPath([user, 'personalInfo.name'])