2012. december 1., szombat

Protain - basics, namespacing

Let's talk about some development things also, like JavaScript and Protain.

This is my favourite language for web usage. And the reason is because JavaScript is flexible and I learned how to live with or how to avoid (mostly) the bad parts of the language. =)

This learning/avoiding process came from my mentor/surpervisor/lead front-end developer Daniel Stocker, in the front-end team at Production Minds by using his JS OOP library called Troop.

That library gave the inspiration to create Protain.

What is Protain?

A JavaScript library that supports namespacing, inheritance and functionality in a much simplier and cleaner, organised way. Namespacing is the first key concept of Protain. Second key concept is sharing functionality between classes through "nodes". Third is to provide an inheritance functionality that is easy to use without breaking the primary key concepts of protain.

Using Protain - namespaces

Let's jump right at the middle!
Use the protain.namespace([path]{String}, [callback]{Function}); method to register a namespace and/or create a scope for your context.
Note: if you don't specify a path for the namespace, it will be invoked with the protain's root namespace!

The following code will show you how to register a namespace:
/**
 * registering a namespace
 */
protain.namespace('hu.benqus.namespace_tutorial', function (path) {
    // stuff ...
});
This will create a namespace inside protain, specified by the path (a string, separated by . [dot] or / [backslash]) and the callback function. If you don't specify any arguments for the protain.namespace() method, it will return the the complete namespace hierarchy. Let's use that to check the whole namespace hierarchy in the WebKit Debugger:
console.log(protain.namespace());
Each namespace contents 4 attributes, except the root, which does not contain the path attribute:
  • classes - (surprisingly) classes will be registered right here
  • context - this will be the context of the callback method you specifiy for the above protain.namespace method
  • nodes - nodes are the attributes shared by classes, we will talk about this later
  • path - the current namespace's path from the root

You may also want to have references to another namespaces in the context:
protain.namespace(function (path) {
    //reference to other namespace
    var other = protain.namespace('hu.benqus.other');
});
Namespacing is that simple in protain.
Next tutorial will be about explaining and creating the above mentioned nodes. Stay tuned! =)

Find Protain on GitHub: https://github.com/benqus/protain

Nincsenek megjegyzések:

Megjegyzés küldése