NodeList is a live or static collection

When I am implementing Dynamic Select Menu in a Rails application, I found out that NodeList is a live collection that will automatically update the collection if there is any changes in the DOM.

For example, see this code snippet from MDN documentation:

var parent = document.getElementById('parent');
var child_nodes = parent.childNodes;
console.log(child_nodes.length); // let's assume "2"
parent.appendChild(document.createElement('div'));
console.log(child_nodes.length); // outputs "3"

However, if you use document.querySelectorAll to select elements, the NodeList returned will be static.

For more, refer to the MDN web docs.


document.getElementsByClassName and document.getElementsByTagName, on the other hand, return HTMLCollection which is always live.

If you want to know more about this, refer to this stackoverflow answer.

Generating Slug in Rails Model

Say you have a model Post that contain columns slug. In Rails, you can easily slugify your model using parameterize.

string = "This is a post title"
string.parameterize
#=> "this-is-a-post-title"

string_with_dot = "Post. Title."
string_with_dot.parameterize
#=> "post-title"

To automatically slugify slug for model, you add a before_validation block:

class Post < ApplicationRecord
  before_validation do
    self.slug = self.title.parameterize
  end
end

With this code, it will automatically slugify your Post model before validation is carried out.

For more details, refer to the documentation here

Tags: rails

Checking for Vim Shell

Sometimes, it’s easy to forget when you’re using :sh from Vim. One way to check whether the current shell was started from Vim is the $VIMRUNTIME environment variable.

Normal shell:

$ echo $VIMRUNTIME
 

Shell from Vim:

$ echo $VIMRUNTIME
/usr/local/share/vim/vim81

See original question here.

Forcing Newline in Markdown

Instead of <br />, you can use two spaces to force a newline.

Markdown:

Hello world.  (<-- 2 spaces)
Bye world.

Output:

Hello world.
Bye world.

Info on Viminfo

The .viminfo file is used by Vim to keep track of persistent information across sessions. This includes things like command like history, registers and global marks.

Sometimes, when you find yourself running Vim in multiple sessions at the same time, these information might not be synchronised properly. This is because Vim only reads and writes .viminfo when it launches and quits respectively. To manually read and write .viminfo, use :rviminfo and :wviminfo.

A good use case is global marks. When you save a global mark in Vim Session #1, you won’t be able to jump to it in Vim Session #2 yet. To solve the problem, we do :wviminfo in Vim Session #1 to force Vim to record the new global mark. Then, we do :rviminfo in Vim Session #2 to allow Vim to register the new global mark.

Tags: vim