This week, I ran into classList API, properly. I’ve read about it in the past, but never had the chance to use it. Most of the articles on google, just give you an introduction into classList API, but fail to reveal the full picture. For instance, I was trying to add multiple classes to an element in the DOM and kept getting errors. None of the articles could solve the mystery behind it.
I decided to do my own research and ended up on the current DOM spec.
classList API is a convenient way to access and manipulate the classes for an element in the DOM. In the background, it implements DOMTokenlist interface. The interface clears all the confusion and below is the code to add multiple classes:
Can you spot any issues with the way the API works?
Well, it doesn’t work like jQuery’s (or zepto) API for adding classes. In popular javascript frameworks, you will do something like this:
$('#some-element-id').addClass("class1 class2");
Does it have to work like jQuery? It doesn’t have to work like any of the javascript frameworks out there, but I was expecting it would try and stay close to the existing trend. But, it doesn’t. Anyways, I continue to live and learn.
I use pbcopy (on mac) to copy the current working directory path:
pwd | pbcopy
But, the following redirection doesn’t work:
pbpaste | cd
This command will just take me back to the my home directory. Why does this redirection not work?
Replacing cd with less works as normal, that is, the output of pbpaste is given to the next command as input:
pbpaste | less
The reason cd doesn’t work with redirection is because cd is not an external command. It’s a shell builtin function and it runs in the context of the current shell, and not as an external commands, in a different process. The right way to use cd with pbpaste is to do this:
cd $(echo `pbpaste`)
Hope it helps.
Update (2014-10-20) After recently reading Unix Shell in Ruby, I think the builtin commands can be explained much better with the following excerpt I took from the book:
Every shell has some built in commands. These are commands which are inherently different from typical utilies in that they change the state of the shell and run in the shell process itself. For instance, a utility like cat simply prints text to the terminal. It does not change the state of the shell itself. The same goes for common commands like grep, tail, and curl. But there are certain commands that need to change the state of the shell, and cd is one of them.
I’ve experienced some OutOfMemoryError on my JRuby app and all the googling has led me to one solution, that is, pass the -Xmx, -Xss, and -Xms options to JVM. Now, it’s easy to do it with Java, or JRuby as you just pass it in, but I’m using trinidad and just run:
bundle exec rackup -s trinidad
How do I pass any JVM options in this command?
A little bit of searching in trinidad’s wiki, found a performance tuning page, which tells a way to do it but you have to run the app via jruby command, and not using bundle exec. I can’t do that because my gem and other dependencies for it are managed by bundler. Well, here’s a way to do that:
Well, this is really a note for self because I tend to remember things better if I write them down and what better place than my blog. So, whenever you register a library with bower, please don’t forget to create a tag in the remote repository for that version. Bower uses the version to pull the library down from the tagged version in the remote repository on installation.
I’ve been spending time trying to write a PhoneGap application, and bower is kind of bundler for the Javascript world.
I’m not a big fan of developers getting swayed by NoSQL hype and using mongoDB with Rails. Unless there’s a good enough reason, stick to relational databases.
Anyways, I found myself trying to update id of an object in Rails. Tried it in rails console first and it didn’t work as the id is a read-only field. Next stop, mongo console. Don’t ask my why I’m updating the id, because I have to, unfortunately.
Here are the steps:
1. Start mongo console by running ‘mongo’ on the command line (terminal, iterm)
2. Run following commands in order:
show dbs;
use <your-database_name>;
doc = db.<collection>.findOne({_id: ObjectId('required-id')});
doc._id = ObjectId('new-id');
db.<collection_name>.insert(doc);
If you want you can delete the old document, but apparently you can’t update the id. You will have to clone the doc and give it a new id, and then insert into collection, which is what we’ve done.
Took me a little while to get this working, essentially due to my lack of understanding of mongoDB in general.
Update:
In the commands above, I forgot to commit the insertion to the disk. Please don’t forget to run this at the end: