Kevin’s Can

Filed under

tip

 

Basic JavaScript widgets bound to one namespace

I often have the same basic widgets in each application that I work on; some kind of loader, maybe a noticer, custom confirm dialog and so on. At first, I always had a global variable for each widget, making them accessible from any place in the code. But as the application grew, so did the number of widgets, leaving me with a rather polluted global namespace. To avoid this I now always use a basic object to create a namespace, where I reference further to each widget. Example:

Instead of having a global variable for each widget, there is only one, the mighty app. They're still accessible from anywhere though:

This can be expanded a lot, as discussed in a great post by Aaron Newton: http://www.clientcide.com/best-practices/singletons-and-event-arbiters/

Filed under  //   basic object   javascript   namespace   tip  
Loading mentions Retweet

Comments [0]

Number with delimiter in JavaScript

I have a form with figures that can be altered anytime. When numbers are changed, calculations are made with JavaScript to keep statistics current. The problem was, that the stats were first set server-side by the convenient NumberHelper#number_with_delimiter helper method that is provided by Rails, but when new calculations were made client-side, the pretty formatting was destroyed.

The solution was to shamelessly grab the number_with_delimiter logic and put it into a JavaScript function (a bit simplified).

Now I just have to run the new numbers through number_with_delimiter before updating the stats.

 

Filed under  //   helper   javascript   tip  
Loading mentions Retweet

Comments [1]

Element.reload() w/ MooTools

Quick snippets like this makes my day so much more enjoyable. What I like about this one especially, is that I can move the url to the html template and have it set with a route (for other rails guys).

 

Filed under  //   ajax   javascript   mootools   tip  
Loading mentions Retweet

Comments [1]