Skip to content
Alexander Gerasimov edited this page Dec 3, 2015 · 1 revision

Using nexus refinements

nexus refinements are collection transformations such as mapping, filtering and sorting

Consider a collection of country models:

countries = new nx.Collection
  items: [
    name:       new nx.Cell value:'France'
    capital:    new nx.Cell value:'Paris'
    population: new nx.Cell value:66.03 # million
    ...
  ]

Countries filtered by name (only those that include an F):

countries.filter
  filter: (name) -> /F/.test name
  values: (country) -> country.name.value
  # or perhaps sugary shortcut:
  values: ['name']

The filter method returns an instance of nx.RefinedCollection which can be used as a regular collection, e.g. for rendering with nxt.Collection. Since nx.RefinedCollection inherits all refinement methods, it can be further refined, spawning new refined collections effectively producing computation trees for collections.

Notice how values property/function is used to extract cell values from collection items.

Sorting is similar:

collection.sort	
  compare: ([first], [second]) -> first - second
  values: ['population']

Comparators and filter functions are "resetters" for refined collections since if they are changed, the entire collection must be rebuilt by applying them.

Resetters can be nx.Cells instead of plain values:

ascending = ([first], [second]) -> first - second
descending = ([first], [second]) -> second - first

sort = new nx.Cell value:ascending

collection.sort	
  compare: sort
  values: ['population']

Every time you toggle sort's value, refined collection is reset with new sorting order.

Clone this wiki locally