Class: kb.utils

Defined in: src/core/utils.coffee

Overview

Library of general-purpose utilities

Class Method Summary

Class Method Details

~ (ko.observable|ko.observableArray) wrappedObservable(instance)
~ (void) wrappedObservable(instance, observable)

Dual-purpose getter/setter for retrieving and storing the observable on an instance that returns a ko.observable instead of 'this'. Relevant for:

Examples:

var ShortDateLocalizer = kb.LocalizedObservable.extend({
  constructor: function(value, options, view_model) {
    kb.LocalizedObservable.prototype.constructor.apply(this, arguments);
    return kb.utils.wrappedObservable(this);
  }
});

Overloads:

~ (ko.observable|ko.observableArray) wrappedObservable(instance)

Gets the observable from an object

Parameters:

  • instance ( Any ) the owner

Returns:

  • ( ko.observable|ko.observableArray ) — the observable

~ (void) wrappedObservable(instance, observable)

Sets the observable on an object

Parameters:

  • instance ( Any ) the owner
  • observable ( ko.observable|ko.observableArray ) the observable

~ (Model|Collection) wrappedObject(obj)
~ (void) wrappedObject(obj, value)

Note: this is almost the same as kb.utils.wrappedModel except that if the Model doesn't exist, it returns null.

Dual-purpose getter/setter for retrieving and storing the Model or Collection on an owner.

Examples:

var model = kb.utils.wrappedObject(view_model);
var collection = kb.utils.wrappedObject(collection_observable);

Overloads:

~ (Model|Collection) wrappedObject(obj)

Gets the observable from an object

Parameters:

  • obj ( Object|kb.ViewModel|kb.CollectionObservable ) owner the ViewModel/CollectionObservable owning the kb.Model or kb.Collection.

Returns:

  • ( Model|Collection ) — the model/collection

~ (void) wrappedObject(obj, value)

Sets the observable on an object

Parameters:

  • obj ( Object|kb.ViewModel|kb.CollectionObservable ) owner the ViewModel/CollectionObservable owning the kb.Model or kb.Collection.
  • value ( Model|Collection ) the model/collection

~ (Model|ViewModel) wrappedModel(view_model)
~ (void) wrappedModel(view_model, model)

Note: this is almost the same as kb.utils.wrappedObject except that if the Model doesn't exist, it returns the ViewModel itself (which is useful behaviour for sorting because it you can iterate over a kb.CollectionObservable's ko.ObservableArray whether it holds ViewModels or Models with the models_only option).

Dual-purpose getter/setter for retrieving and storing the Model on a ViewModel.

Overloads:

~ (Model|ViewModel) wrappedModel(view_model)

Gets the model from a ViewModel

Parameters:

  • view_model ( Object|kb.ViewModel ) the owning ViewModel for the Model.

Returns:

  • ( Model|ViewModel ) — the Model or ViewModel itself if there is no Model

~ (void) wrappedModel(view_model, model)

Sets the observable on an object

Parameters:

  • view_model ( Object|kb.ViewModel ) the owning ViewModel for the Model.
  • model ( Model ) the Model

~ (kb.Store) wrappedStore(obj)
~ (void) wrappedStore(obj, store)

Dual-purpose getter/setter for retrieving and storing a kb.Store on an owner.

Examples:

var co = kb.collectionObservable(new Backbone.Collection());
var co_selected_options = kb.collectionObservable(new Backbone.Collection(), {
  store: kb.utils.wrappedStore(co)
});

Overloads:

~ (kb.Store) wrappedStore(obj)

Gets the store from an object

Parameters:

  • obj ( Any ) the owner

Returns:

~ (void) wrappedStore(obj, store)

Sets the store on an object

Parameters:

  • obj ( Any ) the owner
  • store ( kb.Store ) the store

~ (kb.Factory) wrappedFactory(obj)
~ (void) wrappedFactory(obj, factory)

Dual-purpose getter/setter for retrieving and storing a kb.Factory on an owner.

Overloads:

~ (kb.Factory) wrappedFactory(obj)

Gets the factory from an object

Parameters:

  • obj ( Any ) the owner

Returns:

~ (void) wrappedFactory(obj, factory)

Sets the factory on an object

Parameters:

  • obj ( Any ) the owner
  • factory ( kb.Factory ) the factory

~ (kb.EventWatcher) wrappedEventWatcher(obj)
~ (void) wrappedEventWatcher(obj, event_watcher)

Dual-purpose getter/setter for retrieving and storing a kb.EventWatcher on an owner.

Overloads:

~ (kb.EventWatcher) wrappedEventWatcher(obj)

Gets the event_watcher from an object

Parameters:

  • obj ( Any ) the owner

Returns:

~ (void) wrappedEventWatcher(obj, event_watcher)

Sets the event_watcher on an object

Parameters:

  • obj ( Any ) the owner
  • event_watcher ( kb.EventWatcher ) the event_watcher

. (void) valueType(observable)

Retrieves the value stored in a ko.observable.

Examples:

var view_model = kb.viewModel(new Model({simple_attr: null, model_attr: null}), {factories: {model_attr: kb.ViewModel});
kb.utils.valueType(view_model.simple_attr); // kb.TYPE_SIMPLE
kb.utils.valueType(view_model.model_attr);  // kb.TYPE_MODEL

See also:

. (String) pathJoin(path1, path2)

Helper to join a dot-deliminated path.

Examples:

kb.utils.pathJoin('models', 'name'); // 'models.name'

Parameters:

  • path1 ( String ) start path.
  • path2 ( String ) append path.

Returns:

  • ( String ) — combined dot-delimited path.

. (Object) optionsPathJoin(options, path)

Helper to join a dot-deliminated path with the path on options and returns a new options object with the result.

Examples:

this.friends = kb.collectionObservable(model.get('friends'), kb.utils.optionsPathJoin(options, 'friends'));

Parameters:

  • options ( Object ) with path property for the start path
  • path ( String ) append path.

Returns:

  • ( Object ) — new options with combined dot-delimited path {path: combined_path}.

. (void) inferCreator(value, factory, path)

Helper to find the creator constructor or function from a factory or ORM solution

. (void) createFromDefaultCreator(obj, options)

Creates an observable based on a value's type.

    Quickly fuzzy find classes, mixins, methods, file:

    Control the navigation frame:

    You can focus and blur the search input: