@frzr/list
v0.0.7
Published
Simple List for frzr
Downloads
2
Readme
list
FRZR ordered list of Models
installation
// with frzr
npm install frzr
// standalone
npm install @frzr/list
require
// with frzr
var List = require('frzr').List
// standalone
var List = require('@frzr/list')
usage
var list = new List(options)
options
- add: triggers when Model is added
- sort: triggers when Model is changed
- remove: triggers when Model is removed
Any other parameter will be added as a local attribute
Example
var items = [{_id: 1, groupA: 1, name: 'Item 1'}, {_id: 2, groupA: 1, groupB: 2, name: 'Item 2'}, {_id: 3, groupB: 2, name: 'Item 3'}]
var CustomModel = Model.extend({
idAttribute: '_id'
})
var collection = new Collection({
model: CustomModel,
idAttribute: '_id'
})
collection.reset(items)
var options = {add: add, sort: sort, remove: remove}
var list = new List(options)
list.reset(collection.find('groupA', 1))
list.reset(collection.find('groupB', 2))
function add (id, item, pos) {
console.log('add:' + id, item, 'to', pos)
}
function sort (id, item, pos, oldPos) {
console.log('sort:' + id, item, 'from', oldPos, 'to', pos)
}
function remove (id, item, pos) {
console.log('remove:' + id, item, 'at' + pos)
}
// or
var CustomList = List.extend(settings)
var list = new CustomList(data)
methods
- reset(models): update Models (add, sort and remove when necessary)
events
- add: triggered when Model is added
- sort: triggered when Model is reordered
- remove: triggered when Model is removed