lai:reactive-number

v1.0.1Published 9 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

ReactiveNumber

Manipulate and fetch a number reactively.

Installation

meteor add lai:reactive-number

Why?

Because I'm sick and tired of doing:

1Session.set('counter', Session.get('counter')++);
2// and
3number.set(number.get()++);

Usage

This is the initial Meteor counter app, but using ReactiveNumber instead of Session.

1if (Meteor.isClient) {
2  // counter starts at 0
3  var counter = new ReactiveNumber(0);
4
5  Template.hello.helpers({
6    counter: function () {
7      return counter.get();
8    }
9  });
10
11  Template.hello.events({
12    'click button': function () {
13      counter.add(1);
14    }
15  });
16}

Here's all the use cases.

1var number = new ReactiveNumber(1);
2number.add(1) // 2
3number.subtract(12); // -10
4number.multiply(-10); // 100
5number.divide(4); // 25
6number.calculate(function (number) {
7  return Math.sqrt(number); // 5
8});

API

new ReactiveNumber(Number);

Instantiates a reactive number. Floats allowed. NaNs not allowed. Defaults to 0 if nothing is passed.

.get()

Returns the value of the number reactively.

.set()

Sets the value of the number and triggers reactivity. The following methods set the value as well.

.add(Number)

.subtract(Number)

.multiply(Number)

.divide(Number)

.calculate(Function)

Need something more elaborate? To create a custom calculation, pass in a function with the parameter as the current value of the ReactiveNumber.