Coder Social home page Coder Social logo

meteor-variable's Introduction

variable

Simple reactive variables that contain EJSON values.

This package encapsulates a simple but 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 complete Session replacement: variables are unnamed and aren’t persisted across hot code pushes like Session key/values are.

Version

1.0.0

Meteor linker supported: This implementation works with both pre-linker Meteor (0.6.4.1 and below) and the new "linker" version of Meteor (0.6.5-rc12 and above).

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.

meteor-variable's People

Contributors

awwx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.