Hacker News new | comments | show | ask | jobs | submit login

Is this similar to backbone.js?

They're two very different approaches to the same problem. To put it roughly: Backbone emphasizes smart models that can be passed around and observed by views or controllers, and Knockout emphasizes smart views that have bindings and logic written into HTML attributes.

The levels of granularity are also different. With Knockout, any change to the data will update that atomic portion of the view. With Backbone, the default is to be much more coarsely grained. The good baseline approach for getting started is to simply re-render the view when the model changes:

    model.bind("change", this.render);
And now, whenever any attribute in the model is updated, the view is redrawn. In most cases, that's all you ever need, but in performance critical cases, you can optimize by listening for changes to specific values, and updating just those portions of the view.

    model.bind("change:title", function(model, newTitle) {
It's great to see different approaches to JS Apps start to surface. I wonder how much cross-pollination there's going to be...

I considered both Backbone and Knockout and ended up with Knockout, although both are great! I find Knockout's simple declarative binding to be cleaner for most cases:

  <span data-bind:"text: title"></span>
And Knockout allows you to put bindings in code too, where that makes sense. I also like the way Knockout keeps everything in the view model, rather than passing in the observable attributes when constructing an instance. For instance:

  var Sidebar = Backbone.Model.extend({
    promptColor: function() {
      var cssColor = prompt("Please enter a CSS color:");
      this.set({color: cssColor});
Here color doesn't exist yet. I guess this is just standard javascript style? In Knockout (+coffeescript), I'd write:

  class SideBar
    color: ko.observable('white')
    promptColor: ->
       @color(prompt('Please enter CSS color:'))
My model is self-contained. This example also shows the syntax for updating and getting values. I prefer Knockout's

to Backbone's

@set({color: cssColor})

although this is minor and perhaps you want to allow for bulk updates.

I can't help but feel that these types of libraries are the future. I hope there is lots of cross-pollination going on. Sproutcore focuses on the 5 percent of webapps that should be desktop style. Both Knockout and Backbone give lots of the same benefits, but allow a more flexible application style.

My only wish is for a language agnostic persistence protocol, with some socket.io and REST protocols implemented for most frameworks/languages. Ideally, I could use this protocol to sync with my iPhone/Android/HTML5 app, without changing the server side. This could get very hairy (FeedSync/LiveMesh/Wave), but something simple that accomplishes 90% would be a great.

Knockout is meant to enable the whole MVVM pattern, and supports it with:

* Declarative binding syntax (a very simple way to link model properties/functions to DOM elements without writing manual "render" functions or event handlers) * Automatic dependency tracking (so you can have arbitrary nested chains of computed properties that combine multiple observables, and so KO can work out on its own exactly which part of your UI to refresh after any given model change).

The templating stuff is a bit more baked in with KO too. Backbone, on the other hand, does more of the JSON data access stuff natively, whereas KO assumes you'll use normal jQuery Ajax for that.

(Disclaimer: I don't know backbone.js well, and I created Knockout, so you may want to make your own comparison.)

I don't think so. Backbone is more about the Models, Collections, and persistence. Backbone doesn't do much with views or even touch templating, it just gives you a basic way to hook up the views to the data.

From what it looks like Knockout is all about the views and templating. Seems like the killer feature is auto-updating views and its templating system.

Backbone does do some things with callbacks and handling changes to your models, but you would update the views yourself.

I only have looked at Knockout for about 30 seconds but it does seem to be in the same space as backbone.js.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact