awwx:variable

Reactive variables that contain EJSON values


License
MIT
Install
meteor add awwx:variable@=1.1.0

Documentation

Obsolete

The functionality of this package is now provided by the core Meteor package reactive-var:

var weather = new ReactiveVar("sunny", EJSON.equals);

variable

Reactive variables that contain EJSON values.

This package encapsulates a common case for reactive dependencies: when you want to have a simple variable that contains an EJSON-compatible value, and you'd like that variable to be a reactive data source.

When set, the variable only triggers a reactive update if the new value is actually different than the old one.

var weather = new Variable("sunny");

// prints "weather: sunny" as the autorun runs for the first time.
Deps.autorun(function () {
  console.log("weather: ", weather());
});

weather.set("sunny");    // nothing printed, the value hasn't changed
weather.set("hailing");  // prints "weather: hailing"

Note that while variables are similar to Meteor’s Session object in that they provide a reactive data source, variables are not a Session replacement: variables are unnamed and aren’t persisted across hot code pushes like Session key/values are. Instead, variables are a basic building block for developing reactive applications.

Version

1.1.0

Built for the Meteor package system. To install:

meteor add awwx:variable

API

new Variable()

[new] Variable(initialValue)   Anywhere

Creates and returns a new variable. The JavaScript new keyword is optional.

Arguments

initialValue: EJSON-compatible value
The initial value for the variable.

variable()

variable()   Anywhere

Returns the current value of the variable. A reactive data source.

variable.set()

variable.set(value)   Anywhere

Sets the value of the value. Triggers a reactive update (invalidates dependent computations) if the new value is different than the old.

Arguments

value: EJSON-compatible value
The new value for the variable.

Using with isolateValue

The isolate-value package can be used to further isolate reactive updates, if needed.

var post = new Variable({title: "Greetings", status: "draft"});

function title() {
  return isolateValue(function() {
    return post().title;
  });
}

// does not trigger an invalidation of a computation using title()

post.set({title: "Greetings", status: "published"});

Donate

An easy and effective way to support the continued maintenance of this package and the development of new and useful packages is to donate through Gittip.

Gittip is a platform for sustainable crowd-funding.

Help build an ecosystem of well maintained, quality Meteor packages by joining the Gittip Meteor Community.

Hire

Need support, debugging, or development for your project? You can hire me to help out.