node package manager

IIF

What is this?

Well, i've gotten mighty tired of the conventional programming patterns, and i stumbled upon a very intrested ideea in Alex McCaw's book about Web Applications, and decided to walk on that path.

What's the catch?

With every object that inherits the model, you get to construct its controller at the same time, from the same object. I'll show you...

Here is how you would create a Users model :

class User extends require("Model")  // using CommonJS here to get the module
    @extend require("EventHandler") // Mixin the EventHandler into the controller
    @include require("EventHandler") // Mixin the EventHandler into the model
    / These methods belong controller part of the object /
    @getAllUsers: (data) -> // fetches data from the serverlet</span></span><span>s say</span><span class="meta delimiter object comma js"><span>,</span></span><span> as a callback to a </span><span class="variable other object js"><span>socket</span></span><span class="meta delimiter period js"><span>.</span></span><span class="variable other js"><span>io</span></span><span> </span><span class="support class js"><span>event</span></span></span></span></div><div class="line"><span class="source coffee"><span class="string quoted script coffee"><span>    @removeAllUsers</span><span class="keyword operator js"><span>:</span></span><span> </span><span class="meta brace round js"><span>(</span><span>)</span></span><span> </span><span class="keyword operator js"><span>-</span></span><span class="keyword operator comparison js"><span>&gt;</span></span><span>  </span><span class="comment line double-slash js"><span class="punctuation definition comment js"><span>//</span></span><span> removes all of the users stored on the client side</span><span> </span></span></span></span></div><div class="line"><span class="source coffee"><span class="string quoted script coffee"><span>    @fetchUsersFromLocalStorage</span><span class="keyword operator js"><span>:</span></span><span> </span><span class="meta brace round js"><span>(</span><span>)</span></span><span> </span><span class="keyword operator js"><span>-</span></span><span class="keyword operator comparison js"><span>&gt;</span></span><span>  </span><span class="comment line double-slash js"><span class="punctuation definition comment js"><span>//</span></span><span> recover users saved in the HTML5 LocalStorage</span><span> </span></span></span></span></div><div class="line"><span class="source coffee"><span class="string quoted script coffee"><span>    </span><span class="comment block js"><span class="punctuation definition comment js"><span>/*</span></span><span> And now, these methods belong to the individual instances of the object, the *model* </span><span class="punctuation definition comment js"><span>*/</span></span></span></span></span></div><div class="line"><span class="source coffee"><span class="string quoted script coffee"><span>    init</span><span class="keyword operator js"><span>:</span></span><span> </span><span class="meta brace round js"><span>(</span></span><span>json</span><span class="meta brace round js"><span>)</span></span><span> </span><span class="keyword operator js"><span>-</span></span><span class="keyword operator comparison js"><span>&gt;</span></span><span>  </span><span class="comment line double-slash js"><span class="punctuation definition comment js"><span>//</span></span><span> Do something with the data </span><span> </span></span></span></span></div><div class="line"><span class="source coffee"><span class="string quoted script coffee"><span>    setName</span><span class="keyword operator js"><span>:</span></span><span> </span><span class="meta brace round js"><span>(</span></span><span>string</span><span class="meta brace round js"><span>)</span></span><span> </span><span class="keyword operator js"><span>-</span></span><span class="keyword operator comparison js"><span>&gt;</span></span><span>  </span><span class="comment line double-slash js"><span class="punctuation definition comment js"><span>//</span></span><span> Set the name of the individual user</span><span> </span></span></span></span></div><div class="line"><span class="source coffee"><span class="string quoted script coffee"><span>    </span><span class="comment block js"><span class="punctuation definition comment js"><span>/*</span></span><span> And so forth </span><span class="punctuation definition comment js"><span>*/</span></span></span></span></span></div></pre></div> <p>Oh and, here is how your getAllUsers:</p> <div class="highlight coffeescript"><pre class="editor editor-colors"><div class="line"><span class="source coffee"><span>    </span><span class="keyword operator coffee"><span>...</span></span></span></div><div class="line"><span class="source coffee"><span>    </span><span class="keyword operator coffee"><span>/</span><span>*</span></span><span> Let</span><span class="string quoted script coffee"><span class="punctuation definition string begin coffee"><span>s suppose the data is transfered using jsons in the form of id: userdata */
    @getAlLUsers: (data) ->
        @create(userdata, id) for id, userdata of data 
            // the @create method creates a new model, and calls the init method of the model created 
    ...

Because we extended the controller with EventHandler object, we can now create a new event to handle the user data.

...
require("EventHandler")  // Making sure the EventHandler is registered
EventHandler.subscribe "getAllUserData"User.getAllUsers
IO = socket.io.connect("some_address") // Connecting to a WebSocket
IO.on "getAllUserData"(err, json) -> // Handling the event
    if err then trigger err // Taking care of the errors
    EventHandler.publish "getAllUSerData"json // Triggering the event
...

Starting from this, we can use the event substructure to make the @removeAllUsers easier :

    ...
    subscribe "removeAllObjects"@remove  // Subscribe each model to the removeAllObjects event
    ...
    @removeAllUsers() -> 
        @publish "removeAllObjects" // Trigger the removeAllObjects event
    ...

When the removeAllObjects event is triggered on the user controller, all of the users will react to it, removing themselves automatically.

You see, between controllers and events, there is nothing easier than programming in CoffeeScript :)

Anyways, currently only a StateMachine is implemented to demonstrate the EventHandler and the Model classes.

Have fun :)

What is this?

Well, i've gotten mighty tired of the conventional programming patterns, and i stumbled upon a very intrested ideea in Alex McCaw's book about Web Applications, and decided to walk on that path.

What's the catch?

With every object that inherits the model, you get to construct its controller at the same time, from the same object. I'll show you...

Here is how you would create a Users model :

class User extends require("Model")  // using CommonJS here to get the module
    @extend require("EventHandler") // Mixin the EventHandler into the controller
    @include require("EventHandler") // Mixin the EventHandler into the model
    /* These methods belong *controller* part of the object */
    @getAllUsers: (data) -> // fetches data from the serverlet`s say, as a callback to a socket.io event
    @removeAllUsers: () ->  // removes all of the users stored on the client side 
    @fetchUsersFromLocalStorage: () ->  // recover users saved in the HTML5 LocalStorage 
    /* And now, these methods belong to the individual instances of the object, the *model* */
    init: (json) ->  // Do something with the data  
    setName: (string) ->  // Set the name of the individual user 
    /* And so forth */

Oh and, here is how your getAllUsers:

    ...
    /* Let`s suppose the data is transfered using jsons in the form of *id*: _userdata_ */
    @getAlLUsers: (data) ->
        @create(userdata, id) for id, userdata of data 
            // the *@create* method creates a new model, and calls the init method of the model created 
    ...

Because we extended the controller with EventHandler object, we can now create a new event to handle the user data.

...
require("EventHandler")  // Making sure the EventHandler is registered
EventHandler.subscribe "getAllUserData"User.getAllUsers
IO = socket.io.connect("some_address") // Connecting to a WebSocket
IO.on "getAllUserData"(err, json) -> // Handling the event
    if err then trigger err // Taking care of the errors
    EventHandler.publish "getAllUSerData"json // Triggering the event
...

Starting from this, we can use the event substructure to make the @removeAllUsers easier :

    ...
    subscribe "removeAllObjects"@remove  // Subscribe each model to the removeAllObjects event
    ...
    @removeAllUsers() -> 
        @publish "removeAllObjects" // Trigger the removeAllObjects event
    ...

When the removeAllObjects event is triggered on the user controller, all of the users will react to it, removing themselves automatically.

You see, between controllers and events, there is nothing easier than programming in CoffeeScript :)

Anyways, currently only a StateMachine is implemented to demonstrate the EventHandler and the Model classes.

Have fun :)