-
Notifications
You must be signed in to change notification settings - Fork 395
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
ractive.unset(keypath) would be great! #1649
Comments
You can use `ractive.update('obj.key') with delete, and performance seems pretty quick: http://jsfiddle.net/eswmv63o/. You could put that logic in an observer (http://jsfiddle.net/eswmv63o/2/): r.observe('foo.*', function(n, o, k, property){
if(typeof n === 'undefined'){
delete r.get('foo')[property];
r.update(k);
}
}, { init: false });
// this now deletes the property as well and updates the view...
r.set('foo.a30'); Or you could create your own Ractive.prototype.unset = function(keypath){
var lastDot = keypath.lastIndexOf( '.' ),
parent = keypath.substr( 0, lastDot ),
property = keypath.substring( lastDot + 1 );
this.set(keypath);
delete this.get(parent)[property];
return this.update(keypath);
}
// property is now deleted and updated...
r.unset('foo.a30'); |
Big +1 on @martypdx's prototype hacking solution. Based on how often I say that, I wonder if it's time we had a proper story around mixins, so that it'd be possible to do things like... require('ractive-mixins-unset'); ...rather than pasting code snippets. But I digress. It would certainly be possible to add this to the core API, but in accordance with #1568 I think we should hold off unless it turns out this is a common need. So I'm going to close this issue, but we can keep the discussion going here by all means - if anyone else out there wishes there was an |
It would be great to be able to use Ractive.js straight with MongoDB's oplog, which reports all the changes like following: {$set: {'a.b.c': 'lorem ipsum'}, $unset: {'a.b.d': true}} So I could use like so: ractive.set(changes.$set) and ractive.unset(changes.$unset) |
Actually, @Rich-Harris I just ran into this need. We've been using Specifically we've been working with video.js, and I check a I verified this by changing it to |
+1 I have wished for unset several times. |
Following the likes of other libraries and normal js convention, might make more sense to call it |
+1 for the feature |
yes, this is very important option! |
now I use delete for object keys:
✨ |
Thanks @martypdx Ractive.prototype.unset = function(keypath){
var parent = '',
property = keypath,
lastDot = keypath.lastIndexOf( '.' );
if(lastDot!==-1){
parent = keypath.substr( 0, lastDot );
property = keypath.substring( lastDot + 1 );
}
this.set(keypath);
delete this.get(parent)[property];
return this.update(keypath);
} |
@peterdenev and @martypdx - thanks :) Just what I was after. I will be using this a lot. |
Any thought on revisiting this as a part of 0.9 api? Definitely something that's been missing on my side as well as the others here. |
I eventually went and did my own view library, which better suits to my needs https://redom.js.org 😎👍 |
Ractive.js is missing
ractive.unset(keypath)
. When you have an object, you can't usedelete
, because binding wouldn't work. Setting the whole object again without the key is fine when you have small object, but when you have large dictionary, it gets slow..You could use
ractive.set(keypath, 'undefined')
, but the key is still there, it's justundefined
.The text was updated successfully, but these errors were encountered: