PyramidJS and webapps
« Tuesday, April 16, 2013 »

javascript knockout pyramidjs

I have to admit that I'm not a big MVC fan. Well not that MVC is a bad thing in itself, but it's funny how people are trying to apply this pattern to everything. In some way, most so called MVC are actually using other patterns like MVP and MVVM. One important goal about these patterns is to reduce coupling between the views, logic and models.

After working on a project made with knockoutjs, I started writing my own framework with knockoutjs. Being a big fan of pyramid, I decided writing a javascript framework taking most ideas from Pyramid. Knockout is a good MVVM and does what it has to do great, but it lacks everything else. Writing code with knockoutjs is fun because you can write code without jQuery and it doesn't feel that bad.

There are some reasons why I chose to use knockout instead of let say backbone. One question to ask yourself is why would I use backbone in the first place? It gives you a “way of doing mvc”. But why would I want MVC? What does it really gives? As far as I can say, the mvc implementation in backbone is just a skeleton for MVC. But it fails when it's time to make things work. Backbone lets you create delegate event to the dom objects….But if the views aren't added to the DOM, it won't bind events. If you delete a view, events binded to the model aren't automatically deleted (while it might have been fixed lately). In short, backbone is a good way to end with a event soup that will kill your time and energy for the sake of writing MVC.

On the other hand, writing code with MVVm can be much simpler. With knockoutjs, everything is handled in knockoutjs. It uses the observer pattern to update anything that has to be updated. Every events are binded by knockout and knockout also knows when to create new DOM elements. Unlike backbone, the viewmodel pattern let us know what we will need to bind. For that reason, events are automatically handled and everything just works. If a field is updated then anywhere where it was displayed is going to get updated and there won't be any element that will get rendered twice or 10000 times. Event dependencies are handled by knockout and everything is fine.

In pyramid, we can use the traversal pattern to get a model. And a function is returning some data to be presented by the view. In javascript it could look like that:

var DocumentList = function (request, context) {
    // Parse document

DocumentList.prototype.documentClicked = function (e) {
    // Handle event

application.addViewModel({context: Document, viewModel: DocumentList})

We could also add some predicates to the addViewModel predicates to get different ModelViews for identical context. I'm not yet sure how I'm going to open some views for some part of the screen. Models needs to belocation aware for things to work.

It's still in progress but some of my progress can be found here in my sandbox site

comments powered by Disqus

Copyright © 2015 Loïc Faure-Lacroix