Knockback.js API and Usage Examples

kb.CollectionObservable

Used to generate ViewModels automatically for a collections's models

Super class: kb.RefCountable

Example:

view_model =
  todos: kb.collectionObservable(new Backbone.Collection([{name: 'name1'}, {name: 'name2'}]))
var view_model = {
  todos: kb.collectionObservable(new Backbone.Collection([{name: 'name1'}, {name: 'name2'}]))
};

kb.collectionObservable(collection, [options]) factory

Used to create a new instance

Parameters
  1. model:{ Backbone.Collection }:the collection to synchronize with model additions, removals, and resorting
  2. options: optional
    • view_model{ constructor(model, options) }:
    • view_model_create{ function(model, options) }:
    • create{ function(model, options) }:
    • sort_attribute{ string }:
    • sorted_index{ function(models | view_models, model_to_add | view_model_to_add) }:
    • defer{ boolean }:

Example:

todos = kb.collectionObservable(new Backbone.Collection([{name: 'name1'}, {name: 'name2'}]))
var todos = kb.collectionObservable(new Backbone.Collection([{name: 'name1'}, {name: 'name2'}]));

constructor(collection, [options]) observable returned

Used to create a new instance

Parameters
  1. model:{ Backbone.Collection }:the collection to synchronize with model additions, removals, and resorting
  2. options: optional
    • view_model{ constructor(model, options) }:
    • view_model_create{ function(model, options) }:
    • create{ function(model, options) }:
    • sort_attribute{ string }:
    • sorted_index{ function(models | view_models, model_to_add | view_model_to_add) }:
    • defer{ boolean }:

Example:

todos = new kb.CollectionObservable(new Backbone.Collection([{name: 'name1'}, {name: 'name2'}]))
var todos = new kb.CollectionObservable(new Backbone.Collection([{name: 'name1'}, {name: 'name2'}]));

collection([collection], [options]) dual purpose

Used to get/set the collection

Parameters
  1. collection{ Backbone.Collection }: optional the collection to synchronize with model additions, removals, and resorting
  2. options: optional
    • silent{ boolean }:

Example:

todos = new kb.CollectionObservable(new Backbone.Collection())

# get
current_collection = todos.collection()

# set
todos.collection(new Backbone.Collection([{name: 'name3'}, {name: 'name4'}]))
var todos = new kb.CollectionObservable(new Backbone.Collection();

# get
var current_collection = todos.collection();

# set
todos.collection(new Backbone.Collection([{name: 'name3'}, {name: 'name4'}]));

sortedIndex(sorted_index, [sort_attribute], [options])

Sets the sorted_index function for sorting Models or ViewModels in the collection observable
# change the sorting function
collection_observable.sortedIndex(
  ((view_models, vm) -> _.sortedIndex(view_models, vm, (test) -> kb.utils.wrappedModel(test).get('name')))
)
// change the sorting function
collection_observable.sortedIndex(
  function(view_models, vm){
    return _.sortedIndex(view_models, vm, (test) -> kb.utils.wrappedModel(test).get('name'));
  }
);

sortAttribute((sort_attribute, [sorted_index], [silent]))

Sets the sort attribute name for the collection observable
todos = new kb.CollectionObservable(new Backbone.Collection([{name: 'Zanadu', name: 'Alex'}]))
# in order of Zanadu then Alex

todos.sortAttribute('name')
# in order of Alex then Zanadu
var todos = new kb.CollectionObservable(new Backbone.Collection([{name: 'Zanadu', name: 'Alex'}]));
// in order of Zanadu then Alex

todos.sortAttribute('name');
// in order of Alex then Zanadu

hasViewModels()

Returns true if the collection observable was initialized with a view model create option (and hence generates view models for each model)
todos1 = new kb.CollectionObservable(new Backbone.Collection())
todos1.hasViewModels()      # false

todos2 = new kb.CollectionObservable(new Backbone.Collection(), {view_model: kb.ViewModel})
todos2.hasViewModels()      # true
var todos1 = new kb.CollectionObservable(new Backbone.Collection());
todos1.hasViewModels();     // false

var todos2 = new kb.CollectionObservable(new Backbone.Collection(), {view_model: kb.ViewModel});
todos2.hasViewModels();     // true