Underscore - List of methods integrated in Knockout

To view full list of underscore's methods, please go to the original documentation page.

Dictionary Methods

Works on spa.observableSet

keys

Return all keys of the dataset as array :
datacontext.Contacts.keys(); // computed
datacontext.Contacts._keys(); // array

values

Return all keys of the dataset as array :
datacontext.Contacts.values(); // computed
datacontext.Contacts._values(); // array

clone

Return a clone of the object :
datacontext.Contacts.clone(); // computed
datacontext.Contacts._clone(); // object

isEmpty

Check whether object is empty or not :
datacontext.Contacts.isEmpty(); // computed
datacontext.Contacts._isEmpty(); // boolean

Collection Methods

Works on ko.observableArray, spa.observableSet, spa.observableView, spa.observableRelation

each

Loop over each value in array and execute iterator function over it :
contacts.each(function(c) { saveItem(c); }); // rerun every time datacontext.Contacts changed
contacts._each(function(c) { saveItem(c); }); // run one time

map

Transform each value in collection by using iterator function :
contacts.map(function(c) { return c.firstName() + " " + c.lastName(); }); // ko.computed
contacts._map(function(c) { return c.firstName() + " " + c.lastName(); }); // [ "Full name1", "Full name2", ...]

select

Transform each value in collection by using iterator function and filter by returning null :
contacts.select(function(c) { if (!c.isDeleted()) return c.firstName() + " " + c.lastName() }); // ko.computed
contacts._select(function(c) { if (!c.isDeleted()) return c.firstName() + " " + c.lastName() }); // [ "Full name1", "Full name3", ...]

reduce

find

Return the first entity which resolve iterator function.
contacts.find(function(c) { return c.firstName() == "Bob"; }); // ko.computed
contacts._find(function(c) { return c.firstName() == "Bob"; }); // { firstName: "Bob", ... }

filter

Return the first entity which resolve iterator function.
contacts.filter(function(c) { return c.isDeleted() === true; }); // ko.computed
contacts._filter(function(c) { return c.isDeleted() === true; }); // array

reject

sum

Return sum of result of iterator function on each value in collection
products.sum(function(p) { return p.price(); }); // ko.computed
products._sum(function(p) { return p.price(); }); // number

average

Return average of result of iterator function on each value in collection
products.average(function(p) { return p.price(); }); // ko.computed
products._average(function(p) { return p.price(); }); // number

all

Return true if all collection values satisfy iterator function.
contacts.all(function(c) { return c.isDeleted(); }); // ko.computed
contacts._all(function(c) { return c.isDeleted(); }); // boolean

any

Return true if any collection values satisfy iterator function.
contacts.any(function(c) { return c.isDeleted(); }); // ko.computed
contacts._any(function(c) { return c.isDeleted(); }); // boolean

contains

Check if collection contains element
contacts.contains(contact); // ko.computed
contacts._contains(contact); // boolean

max

Return element with the maximum value returned by its iterator function
products.max(function(p) { return p.price(); }); // ko.computed
products._max(function(p) { return p.price(); }); // object

min

Return element with the minimum value returned by its iterator function
products.min(function(p) { return p.price(); }); // ko.computed
products._min(function(p) { return p.price(); }); // object

sortBy

Return a new collection sorted using iterator function.
products.sortBy(function(p) { return p.price(); }); // ko.computed
products._sortBy(function(p) { return p.price(); }); // sorted array

groupBy

Return a new collection grouped using iterator function.
products.grouptBy(function(p) { return p.category(); }); // ko.computed
products._groupBy(function(p) { return p.category(); }); // grouped collection

toArray

return an array of every object or collection.
datacontext.Contacts.toArray(); // ko.computed
datacontext.Contacts._toArray(); // array

count

Count value into collection which satisfy iterator function
products.count(function(p) { return p.price() > 300; }); // ko.computed
products._count(function(p) { return p.price() > 300; }); // number

size

Count value into collection
products.size(); // ko.computed
products._size(); // number

Last edited Dec 20, 2012 at 5:38 PM by SomaticIT, version 3

Comments

No comments yet.