Monday 15 April 2013

Underscore.js






Underscore.js is a tiny JavaScript utility library that makes working with some of the common data structures used in JavaScript much easier. Using Underscore is extremely easy. It is not tightly bound to the DOM, doesn’t make any assumptions about other libraries or frameworks that may be in use and doesn’t require any setup or configuration. We can simply include the library file in your page and you can start calling its methods.

The library is composed of about 80 functions that provide functional programming support without extending the built-in javascript objects. What makes this library stand out is that it delegates to built-in functions if our browser or javascript engine supports it. There are several benefits of using underscore.js.

  • A performance boost due to the execution of native code.
  • Your code won’t break even if a particular browser or javascript engine doesn’t yet support the ECMA Script 5 specification.
  • More intuitive and concise javascript code.
Some of the functions out of many are explained here:

_.each
The _.each function allows for iteration through a collection in a more semantically meaningful way and will call the native function if it is supported. Please go through the code given below.

function SomeFunction(item)
{
    //function codes
}
for (index = 0; index < maxCount; ++index)
{
    SomeFunction(collection[index], index);
}
//using _.each
function SomeFunction(item)
{
    //function codes
}
.each(collection, SomeFunction);

As you can see from this example, what needs to be expressed in several lines of code can be done with one line. The code that uses _.each is elegant, far more meaningful, and it will be as efficient as it can be for the given execution environment.

_.map
The _.map function exposes the ability to “map” or transform a list by executing a user specified function. It provides a powerful way to efficiently iterate through a collection performing some operation on each element of the collection. It will also call to the native function if it is supported.


function Transformer(item)
{
   // function codes
}
var transformed = new Array();
for (index = 0; index < maxCount; ++index)
{
   transformed.push(Transformer(collection[index]));
}
// _.map to transform items in an element
function Transformer(item)
{
   // function codes
}
var transformed = _.map(collection, Transformer);

Like the _.each function _.map provides a more expressive and concise syntax for what is truly happening in the code.

_.filter
The _.filter function iterates over the provided collection, returning an array of all the values that meet the criteria of a user specified function.

function FilterCondition(item)
{
 // function codes
}
var selected = new Array();
for (index = 0; index < maxCount; ++index)
{
if (FilterCondition(collection[index]))
   {
      selected.push(collection[index]);
   }
}
// _.filter
function FilterCondition(item)
{
 // function codes
}
var selected = _.filter(collection, FilterCondition);

We can see how compact the old function code has become by using _.filter. We can also see that the use of _.filter forces the user of the function to have better separation of behaviors within their javascript code instead of having massive blocks of code that are at best tough to read and at worst introduce odd side-effects.

The above examples highlights the fact that underscore.js is one of those libraries that we should “just use”. It adds no overhead, is trivial to integrate and makes your code more robust.





[
Expecting Your Valuable Comments]
Thank You





Backbone.js







Backbone is a lightweight Model-View Javascript framework designed to help organise our websites. Backbone is based on the popular Model-View-Controller pattern.

Backbone focuses most of it’s attention on the ModelView and Collection. These are the three main components that allows us to create apps which separate out our data from our interface by storing data in our Models which are combined together in Collections, which are then bound to Views. Models represent the data within an app and can service one or many Views at any one time. Collections group together Models and can make changes across Models, Views are the visual elements we see on the page. The elements/Views listen for changes to the Model and update accordingly.

Backbone has two dependencies in order to work correctly. jQuery and Underscore.js .The Underscore library is a nice utility belt of helpful methods which complement and enhance Backbone.

Models : A Model represents the interactive data, data-logic and validation functionality within an application. It does not need to know about the Views or Collections, it simply retrieves data from a source and then stores the data as an object.

Events : An Event play an important part within an app. Events can be used to communicate to Views when data has changed in the Model, this gives the view the chance to react to the change and update the template accordingly.

List of all of the built-in events:

  • change (Model, options) — when a Model’s attributes have changed.
  • add (Model, Collection) — when a Model is added to a Collection.
  • remove (Model, Collection) — when a Model is removed from a Collection.
  • reset (Collection) — when the Collection’s entire contents have been replaced.
  • destroy (Model, Collection) — when a Model is destroyed.
  • change:[attribute] (Model, value, options) — when a specific attribute has been updated.
  • sync (Model, Collection) — triggers whenever a Model has been successfully synced to the server.
  • error (Model, Collection) — when a Model validation fails, or a save call fails on the server.
  • route:[name] (router) — when one of a router’s routes has matched.
  • all — this special event fires for any triggered event, passing the event name as the first argument.



[
Expecting Your Valuable Comments]
Thank You



Sunday 17 March 2013

MongoDB








Mongo is an open source nosql document-oriented database system which is very different from MySQL. The most considerable difference is that MySQL is written using SQL queries, while MongoDB is focused on BSON (Binary JSON) a binary object format similar to, but more expressive than JSON.

Instead of tables, a MongoDB database stores its data in collections, which are equivalent to RDBMS tables. A collection holds one or more documen, which corresponds to a record or a row in a relational database table, and each document has one or more fields, which corresponds to a column in a relational database table.

MongoDB will create collections and databases on their first use. We do not need to create the database or collection before inserting data.

MongoDB uses dynamic schemas. We can create collections without defining the structure and we also can change the structure of documents simply by adding new fields or by deleting existing ones.

Basic Commands of MongoDB:

* use mydb : Sets the context to mydb database. 
* mydb.mycol.insert( { "name" : "Mongo" } ) : Inserts document into the collection mycol.
* show.collections Shows the existing collections in database.
* mydb.mycol.find( ) : Shows the documents within a collection mycol.
* mydb.mycol.findOne( ) : Retrieves single document from the collection mycol.
* mydb.mycol.find( ).limits( n ) : Retrieves n document from the collection mycol.

Advantages of MongoDB

1Lightening fast.
2Auto sharding on it's way.
3Replication is very easy.
4You can perform rich queries, can create on the fly indexes with a single command.


Disadvantages of MongoDB

1Very unreliable.
2Indexes take up a lot of RAM.


Here you can get the Paint application using MongoDB




[Expecting Your Valuable Comments]
Thank You