peerlibrary/meteor-reactive-field

Name: meteor-reactive-field

Owner: PeerLibrary

Description: Reactive field for Meteor

Created: 2015-06-13 01:05:40.0

Updated: 2018-02-25 22:28:22.0

Pushed: 2018-02-25 22:30:24.0

Homepage: https://atmospherejs.com/peerlibrary/reactive-field

Size: 11

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Reactive field for Meteor

Reactive field for Meteor provides an alternative syntax for ReactiveVar:

field = new ReactiveField('initialValue');
ole.log(field()); // prints 'initialValue' string
d('newValue');
ole.log(field()); // prints 'newValue' string

So instead of field.get() and field.set(), you can simply call it as a function which (reactively) returns a value, or call it with an argument to set the new value. Setter still returns the value.

Optionally, you can pass custom equality function:

ReactiveField('initialValue', function (a, b) {return a === b});

You can also enable storing the previous value stored in the reactive field using:

ou can also pass it as a third argument when using custom equality function
field = new ReactiveField('initialValue', true);
ole.log(field()); // prints 'initialValue' string
d('newValue');
ole.log(field()); // prints 'newValue' string
ole.log(field.previous()); // prints 'initialValue' string

This is useful when you want to compare inside a computation how the value changed. field.previous() is not reactive.

Adding this package to your Meteor application adds the ReactiveField constructor into the global scope.

Both client and server side.

Installation
or add peerlibrary:reactive-field
Motivation

The main motivation for this package is that you can assign reactive values to Blaze Components and then you can access them in the template by simply doing {{field}} instead of {{field.get}}. And same for any other objects for which you create reactive fields in the constructor and then you or have this.field.get() calls all around the code, which is ugly because if you ever decide to convert this.field into a getter with some additional logic you have to change code everywhere. Or you create a this.field() getter in advance, which calls this._field.get() for you, but that is again a lot of work. So the easiest thing is to have reactive fields be methods to begin with so you can change them later on if necessary while keeping backwards compatibility.

Related projects

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.