Class: kb.CollectionObservable
Defined in: | src/core/collection-observable.coffee |
Overview
Base class for observing collections.
Examples:
How to create a ko.CollectionObservable using the ko.collectionObservable factory.
var collection = new Collection([{name: 'name1'}, {name: 'name2'}]);
var view_model = {
todos: kb.collectionObservable(collection)
};
How to access and change the observed collection.
var todos = new kb.CollectionObservable(new kb.Collection([{name: 'name1'}, {name: 'name2'}]);
var current_collection = todos.collection(); // get
todos.collection(new Backbone.Collection([{name: 'name3'}, {name: 'name4'}])); // set
Instance Method Summary
- # (void) destroy() Required clean up function to break cycles, release view models, etc.
- # (void) shareOptions() Get the options for a new collection that can be used for sharing view models.
- # (void) filters(filters) Setter for the filters array for excluding models in the collection observable.
- # (void) comparator(comparator) Setter for the sorted index function for auto-sorting the ViewModels or Models in a kb.CollectionObservable.
- # (void) sortAttribute(sort_attribute) Setter for the sort attribute name for auto-sorting the ViewModels or Models in a kb.CollectionObservable.
- # (void) viewModelByModel(model) Reverse lookup for a view model by model.
- # (void) hasViewModels() Will return true unless created with models_only option.
- # (void) compact() Compacts the Collection Observable to use the least amount of memory.
Constructor Details
#
(ko.observableArray)
constructor(collection, view_model, options)
Used to create a new kb.CollectionObservable.
When the observable is updated, the following Backbone.Events are triggered:
- add: (view_model, collection_observable) or if batch: (collection_observable)
- resort: (view_model, collection_observable, new_index) or if batch: (collection_observable)
- remove: (view_model, collection_observable) or if batch: (collection_observable)
Instance Method Details
#
(void)
destroy()
Required clean up function to break cycles, release view models, etc. Can be called directly, via kb.release(object) or as a consequence of ko.releaseNode(element).
Get the options for a new collection that can be used for sharing view models.
Examples:
Sharing view models for an HTML select element.
var selected_collection = new Backbone.Collection();
var available_collection = new Backbone.Collection([{name: 'Bob'}, {name: 'Fred'}]);
var selected = kb.collectionObservable(available_collection);
var available = kb.collectionObservable(available_collection, available_collection.shareOptions()); // view models shared with selected collection observable
#
(void)
filters(filters)
Setter for the filters array for excluding models in the collection observable.
Examples:
// exclude a single model by id
collection_observable.filters(model.id);
#
(void)
comparator(comparator)
Setter for the sorted index function for auto-sorting the ViewModels or Models in a kb.CollectionObservable.
Examples:
// change the sorting function
collection_observable.comparator(
function(view_models, vm){
return _.comparator(view_models, vm, (test) -> kb.utils.wrappedModel(test).get('name'));
}
);
#
(void)
sortAttribute(sort_attribute)
Setter for the sort attribute name for auto-sorting the ViewModels or Models in a kb.CollectionObservable.
Examples:
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
#
(void)
viewModelByModel(model)
Reverse lookup for a view model by model. If created with models_only option, will return null.
#
(void)
hasViewModels()
Will return true unless created with models_only option.
Examples:
var todos1 = new kb.CollectionObservable(new Backbone.Collection(), {models_only: true});
todos1.hasViewModels(); // false
var todos2 = new kb.CollectionObservable(new Backbone.Collection());
todos2.hasViewModels(); // true
#
(void)
compact()
Compacts the Collection Observable to use the least amount of memory. Currently, this is brute force meaning it releases than regenerates all view models when called.