Knockback.js API and Usage Examples

kb.ViewModel

Used to generate observables automatically for a model's attributes

Super class: kb.RefCountable

Examples:

view_model = kb.viewModel(new Backbone.Model({first_name: "Planet", last_name: "Earth"}))

# Do something then clean up
# kb.utils.release(view_model); view_model = null
var view_model = kb.viewModel(new Backbone.Model({first_name: "Planet", last_name: "Earth"}));

// Do something then clean up
// kb.utils.release(view_model); view_model = null;

Tutorials

kb.ViewModel::extend(properties, [classProperties]) class

Used to derive a class in JavaScript (when CoffeeScript is not used)

Example:

ViewModel = kb.ViewModel.extend({
  constructor: (model) ->
    kb.ViewModel.prototype.constructor.apply(this, arguments)
    @full_name = ko.computed(=>{ return @first_name() + " " + @last_name(); })
    
var view_model = new ViewModel(model);
var ViewModel = kb.ViewModel.extend({
  constructor: function(model){
    kb.ViewModel.prototype.constructor.apply(this, arguments);
    this.full_name = ko.computed(function() { return this.first_name() + " " + this.last_name(); }, this);
  }
});
var view_model = new ViewModel(model);

kb.viewModel(model, [options]) factory

Used to derive a class in JavaScript (when CoffeeScript is not used)

Parameters
  1. model:{ Backbone.Model }:the model whose attributes will be synchronized
  2. options: optional
    • internals{ [strings] }:
    • requires{ [strings] }:
    • children{ function(model, key, options) | {key: function(model, key, options)} }:
    • create{ function(model, key, options) }:
    • read_only{ boolean }:

Example:

view_model = kb.viewModel(model)
var view_model = kb.viewModel(model);

constructor(model, [options])

Used to derive a class in JavaScript (when CoffeeScript is not used)

Parameters
  1. model:{ Backbone.Model }:the model whose attributes will be synchronized
  2. options: optional
    • internals{ [strings] }:
    • requires{ [strings] }:
    • children{ function(model, key, options) | {key: function(model, key, options)} }:
    • create{ function(model, key, options) }:
    • read_only{ boolean }:

Example:

view_model = new kb.ViewModel(model)
var view_model = new kb.ViewModel(model);

model([model]) dual purpose

Used to get/set the model

Parameters
  1. model: { Backbone.Model }: optional the model to set on the view model

Example:

view_model = kb.viewModel(new Backbone.Model({name: 'bob'}))

# get
the_model = view_model.model()

# set
view_model.model(new Backbone.Model({name: 'fred'}))
var view_model = kb.viewModel(new Backbone.Model({name: 'bob'}));

// get
var the_model = view_model.model();

// set
view_model.model(new Backbone.Model({name: 'fred'}));