Well, i've gotten mighty tired of the conventional programming patterns, and i stumbled upon a very intrested ideea in <a href="http://alexmccaw.com/">Alex McCaw's book</a> about Web Applications, and decided to walk on that path.
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.
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 :
"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 */: // fetches data from the serverlet`s say as a callback to a socketio 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 datasetName: 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 ->@ 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 registeredEventHandlersubscribe "getAllUserData"UsergetAllUsersIO = socketioconnect"some_address" // Connecting to a WebSocketIOon "getAllUserData" // Handling the eventif err then trigger err // Taking care of the errorsEventHandlerpublish "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.