Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
README.md

README.md

kompose.js v0.1.3 API Documentation

kp

kp

kp.computedApply(observable, [func], [thisArg])

#

Creates a pureComputed that unwraps and applies the given observable to the given func. A thisArg can be given which is set as the this binding for func. If func is not given, computed returns unwrapped observable value.

Arguments

  1. observable (ko.observable): The observable to apply. This can be any type of knockout observable.
  2. [func] (Function): Function to receive unwrapped observable.
  3. [thisArg] (Object): The this binding of func.

Returns

(Function): A pure computed observable.

Example

function toUpper(string) {
  return string.toUpperCase();
}
var speak = ko.observable('Hello');
var shoutComputed = kp.computedApply(speak, toUpper);
shoutComputed()
// => 'HELLO'
speak('Hello world');
shoutComputed();
// => 'HELLO WORLD'

kp.computedMap(observableArray, [iteratee], [thisArg])

#

Creates a pureComputed that unwraps and maps the given observableArray with the given iteratee function. If iteratee is a string it is treated as a kp.property string.

Arguments

  1. observableArray (ko.observableArray): The observableArray to iterate over.
  2. [iteratee] (Function|Array|String): A function to iterate with, or a string or array property path.
  3. [thisArg] (Object): The this binding of iteratee.

Returns

(Function): A pure computed observable.

Example

function double(n) {
  return n * n;
}
var nums = ko.observableArray([1, 2, 3]);
var doubleComputed = kp.computedMap(nums, double);
doubleComputed();
// => [2, 4, 6]
nums.push(4);
doubleComputed();
// => [2, 4, 6, 8]

var users = ko.observableArray([
  { name: 'Jake', age: ko.observable({ years: 31, months: 5 }) },
  { name: 'Finn', age: ko.observable({ years: 14, months: 2 }) }
]);
var userAgeYearsComputed = kp.computedMap(users, 'age.years');
userAgeYearsComputed();
// => [31, 14]

kp.get(object, path, [defaultValue])

#

Get a value from the given object and path. Unwraps any observables along the path. Optionally pass a defaultValue if path is not available.

Arguments

  1. object (Object): Object to query.
  2. path (Array|String): Path in object to retrieve value from.
  3. [defaultValue] (*): A default value to return if path is not available.

Returns

(*): Value at path or defaultValue.

Example

var object = ko.observable({
  a: { b: ko.observable('c') }
})

kp.get(object, 'a.b');
// => 'c'

kp.get(object, 'foo.bar', 'wibble');
// => 'wibble'

kp.matchesProperty(path, matchValue, [customMatcher])

#

Creates a function that tests if the value at path is the same as the given value. By default it uses === equality. A custom matcher function can be given. Any observables along the path are unwrapped.

Arguments

  1. path (Array|String): Path in object to test.
  2. matchValue (*): Value to match with retrieved value.
  3. [customMatcher] (Function): A custom matcher function that receives two values to match.

Returns

(Function): Returns a new function.

Example

var hikers = [
  {
    name: ko.observable('Ford Prefect'),
    age: ko.observable(200)
  },
  {
    name: ko.observable('Arthur Dent'),
    age: ko.observable(31)
  },
];
_.find(hikers, kp.matchesProperty('age', 200));
// => { name: ko.observable('Ford Prefect'), age: ko.observable(200) };

var obj = { foo: ko.observable({ bar: 'baz' }) };
kp.matchesProperty('foo', { bar: 'baz' }, _.isEqual)(obj);
// => true

kp.method(path, [args])

#

Creates a function that invokes the method at path with the given args. Unwraps any observables along the path.

Arguments

  1. path (Array|String): The path of the method.
  2. [args] (...*): Zero or more arguments to apply to the method.

Returns

(Function): Returns a new function.

Example

var objects = [
  { a: ko.observable({ b: function (val) { return val + 1; } }) },
  { a: ko.observable({ b: function (val) { return val + 2; } }) },
];
_.map(objects, kp.method('a.b', 2));
// => [3, 4]

kp.property(path)

#

Creates a function that returns the value at the given path, unwrapping any observables along the way.

Arguments

  1. path (Array|String): The path of the property to get.

Returns

(Function): Returns a new function.

Example

var puppy = { name: ko.observable('Django') };
var getName = kp.property('name');
getName(puppy);
// => 'Django'

var puppies = ko.observableArray([
  { name: ko.observable('Django'), age: ko.observable({ years: 4, months: 2 }) },
  { name: ko.observable('Henry'), age: ko.observable({ years: 2, months: 6 }) }
]);
_.map(puppies, kp.property('age.years'));
// => [4, 2]