@frzr/collection

frzr Collection

Stats

StarsIssuesVersionUpdatedCreatedSize
@frzr/collection
0.1.66 years ago6 years agoMinified + gzip package size for @frzr/collection in KB

Readme

collection

FRZR Collection inspired by Backbone.js

installation

// with frzr
npm install frzr

// standalone
npm install @frzr/collection

require

// with frzr
var Collection = require('frzr').Collection

// standalone
var Collection = require('@frzr/collection')

usage

var collection = new Collection(options)

options

  • idAttribute: attribute to use as id
  • index: attributes to index
  • add: triggers when Model is added
  • change: triggers when Model is changed
  • remove: triggers when Model is removed
  • move: triggers when Model is reordered
  • model: custom extended model

Any other parameter will be added as a local attribute

Example

var options = {idAttribute: 'a'}
var CustomModel = Model.extend(options)

var items = [{a: 1, b: 2}, {a: 2, b: 3}]
var options = {
  idAttribute: 'a',
  index: 'b',
  add: add,
  change: change,
  remove: remove,
  model: CustomModel
}

var collection = new Model(options)
collection.reset(items)

function add (model) {
  console.log('added', model)
}
function change (model) {
  console.log('changed', model)
}
function remove (model) {
  console.log('removed', model)
}
collection.reset([{
  a: 2,
  b: 3,
  c: 4
}, {
  a: 3,
  b: 4
}])

// or
var CustomCollection = Collection.extend(settings)
var collection = new CustomCollection(data)

methods

  • get(id): get model by id
  • set(items): set models
  • reset(items): reset models (remove if necessary)
  • unset(id): remove model by id

events

  • add: triggered when Model is added
  • change: triggered when Model is changed
  • move: triggered when Model is reordered
  • remove: triggered when Model is removed

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.