IIF

npm install IIF
3 downloads in the last month

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 server, let`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 :)

npm loves you