lai:reactive-number

v1.0.0Published 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.

.add(Number)

.subtract(Number)

.multiply(Number)

.divide(Number)

.calculate(Function)

Pass in a function with the parameter as the current value of the ReactiveNumber.