Disable ctrl + n and other ctrl + key combinations in JavaScript

Few days ago Vijay asked if there’s a way to disable ctrl + n combination (open the new window shortcut. So I set out to create a small Java Script that disables any ctrl + key combination (e.g. ctrl + v, ctrl + c, ctrl + a, etc.).

Script is a bit more complicated than Disable form submit on enter keypress and it should work in both Fire Fox and Internet Explorer.

Continue reading

Advertisements

Organizing Your Backbone.js Application With Modules

If you have spent any time looking at Backbone.js, like many others, you are probably amazed by how lightweight, flexible and elegant it is. Backbone.js is incredibly powerful, but not prescriptive in how it should be used. With great power comes great responsibility, and if you’ve tried to use Backbone.js for a large project you might be asking yourself: how do I organize my code?

When thinking about “code organization” the questions you might be asking are:

  1. How do I declare and invoke Backbone types?
  2. How do I manage a separation of concerns?
  3. How do I define a clear entry point to my application?
  4. How do I pick a clear and consistent pattern to organize the code on the file system?
  5. How do I actually name my models, collections, views and routers?

All good questions! Is there an accepted answer? Nope. Continue reading

Reliable Delivery Pub/Sub Message Queues with Redis

Redis-title

 

Redis is a high performance key-value datastore that differs from other key-value solutions in the way it handles values. Instead of just storing values as simple strings, it recognizes multiple specific data types such as Lists, Sets, Hashes (maps), Strings or Numbers. Each data type has its own set of features to manipulate the data it contains in an atomic manner, making it an ideal tool for highly distributed system where concurrency is a potential issue.

Combining those features in creative ways allows for novel ways of doing “traditional” things differently. One particular combination has recently allowed us to implement a moderately (read: good enough) reliable message delivery mechanism for multiple consumers consuming messages at their own pace.  Continue reading

CodeMirror’s mode system

A CodeMirror mode is a module that helps CodeMirror, a code editor, highlight and optionally smart-indent text in a specific programming language or other kind of structured text format.

Code editors take widely different approaches to the way syntax highlighting styles are defined. An elegantly simple approach is outlined in Patrick Walton’s Common JavaScript Syntax Highlighting Specification, basically defining a state machine with regular expressions as its edges. Unfortunately, this proposal was never widely adopted. ACE uses a similar, though incompatible system. Other, more heavyweight, and often downright obscure, systems are found in EmacsVim, or Kate. Continue reading

defineMode IN codemirror

I’m learning some CodeMirror. This is not the first time i try to learn CodeMirror, i had some unsuccessful attempts in the past, but after a lot of trial and errors trying to create my own highlight text editor in javascript, finally i adquire the knowledge to understand it a little bit better to the point were i was able to learn from it.

So, the first thing i didn’t understood about this (because all the code parsing was a new concept for me) was how the mecanism worked.  I was too n00b to understand the documentation, so now, here i will explain what i understood in simple terms in case someone else out there is trying to learn it but requires an easy introduction. This is for people wanting to create their own, custom syntax highlighters. If you want to use it for existing languages, read the “how to use” documentation from codemirror, they have really good examples. Continue reading

This, bind, that in the callback of javascript, node.js

If you want to pass the current object to the callback function in any operation (read file, connect database…)

There are three main ways to deal with this in callbacks:

1. Create a normal variable, as you are currently doing

The most common names for the variable are that and self. I prefer that because in the browser there is a global window property called self that I’d rather not shadow.

function edit(req, res) {
    var that = this,

    db.User.findById('ABCD', function(err, user){
        that.foo(user);
    });
}; Continue reading