pwix:collection-behaviors - README
What is it ?
This package is a fork from zimme:collection-behaviors as its last released version v 1.1.3 from 2015.
It has been forked to make it compatible with Meteor 2. and incoming Meteor 3.x.
Installation
meteor add pwix:collection-behaviours
Following is the original documentation from zimme...
From which I have removed the obsolete parts.
Behaviours for collections
Define and attach behaviours to collections.
Available behaviours
The behaviours are split into separate packages, which depend on this package.
-
zimme:collection-softremovable
This behaviour adds
.softRemove()
and.restore()
to collections, which make it possible to mark documents as removed. It also tracks the time and user for the last soft remove and restore. -
zimme:collection-timestampable
This behaviour timestamps documents on insert and update. It also tracks the user who made the last insert or update.
Usage
Define a behaviour
1CollectionBehaviours.define('behaviourName', function(options) { 2 var collection = this.collection; 3 4 // Setup some default options for the behaviour 5 var defaultOptions = { 6 exampleOption: "I'm a default value" 7 }; 8 9 // Make the behaviour configurable both globally and locally and uses the 10 // defaults if not configured. 11 options = _.defaults(options, this.options, defaultOptions); 12 13 // Behaviour logic goes here 14});
Attach behaviours
1// Attach a behavour using the collection identifier 2Meteor.users.attachBehaviour('timestampable'); 3 4// Attach a behaviour to a colleciton using CollectionBehaviours 5CollectionBehaviours.attach(Meteor.users, 'timestampable'); 6 7// Attach multiple behaviours to a collection with default options 8CollectionBehaviours.attach(Meteor.users, ['timestampable', 'softremovable']); 9 10// Attach multiple behaviours to a collcetion with custom options 11CollectionBehaviours.attach(Meteor.users, { 12 timestampable: { 13 createdAt: 'insertedAt', 14 }, 15 softremovable: { 16 removedBy: 'deletedBy', 17 }, 18}); 19 20// Attach a behaviour to multiple collections 21CollectionBehaviours.attach([Meteor.users, Posts], 'timestampable'); 22 23// Attach multiple behaviours to multiple collections 24CollectionBehaviours.attach( 25 [Meteor.users, Posts], 26 ['timestampable', 'softremovable'] 27);
Configuration
1// Configure behaviour globally i.e. set you own defaults 2CollectionBehaviours.configure('behaviourName', { 3 exampleOption: "I'm a global value" 4}); 5 6// Attach behaviour with custom options 7Meteor.users.attachBehaviour('behaviourName', { 8 exampleOption: "I'm a local value" 9}); 10 11// Attach behaviour with custom options, using CollectionBehaviours 12CollectionBehaviours.attach(Meteor.users, 'behaviourName', { 13 exampleOption: "I'm a local value" 14});
API
CollectionBehaviours.define
Used to defined a new behaviour or overwrite an already defined behaviour.
1CollectionBehaviours.define('behaviourName', behaviourFunciton, options);
'behaviourName'
: Required. The name of the behaviour.behaviourFunction
: Required. AFunction
that takesoptions
as an argument. This function is the behaviour.options
: Optional.Object
with the options for the behaviour.
Options
replace
: Optional. Set totrue
to replace a previously defined behaviour.
CollectionBehaviours.configure
Used to confgure behaviours globally.
1// Configure single behaviour 2ColectionBehaviours.configure('behavioursName', options); 3 4// Configure multiple behaviours 5CollectionBehaviours.configure({ 6 timestampable: { 7 createdAt: 'insertedAt', 8 updatedBy: 'modifiedBy' 9 }, 10 softremovable: { 11 removed: 'deleted' 12 } 13});
-
'behaviourName'
: Required.
If set to aString
, 'behaviourName', will configure the named behaviour.
If set to anObject
, where the keys are named behaviours and the values are
the options for the behaviours, will configure those named behaviours. -
options
: Optional ifbehaviourName
is anObject
.
See specific behaviour for available options.
<CollectionIdentifier>.attachBehaviour
Used to attach behaviour(s) to the collection.
1Meteor.users.attachBehaviour(behaviourNameOrFunction, options);
behaviourNameOrFunction
: Required.
If set to aString
,'behaviourName'
, will attach the named behaviour.
If set to aFunction
, will attach that function as an anonymous behaviour.
If set to anArray
ofString
/Function
, will attach those named or
anonymous behaviours.
If set to anObject
, where the keys are named behaviours and the values are
the behaviours' options, will attach those named behaviours with the provided
options.options
: Optional. See specific behaviour for available options.
CollectionBehaviours.attach
Used to attach behaviour(s) to collection(s).
1CollectionBehaviours.attach(ColletionIdentifier, behaviourNameOrFunction, options);
CollectionIdentifier
: Required. The collection orArray
of collections
you want to attach the behaviour(s) to.behaviourNameOrFunction
: Required.
If set to aString
,'behaviourName'
, will attach the named behaviour.
If set to aFunction
, will attach that function as an anonymous behaviour.
If set to anArray
ofString
/Function
, will attach those named or
anonymous behaviours.
If set to anObject
, where the keys are named behaviours and the values are
the behaviours' options, will attach those named behaviours with the provided
options.options
: Optional. See specific behaviour for available options.
Notes
CollectionBehaviours.config
is an alias forCollectionBehaviours.configure
- The inspiration for this package came from