Giter Site home page Giter Site logo

edushareontario / meteor-reactive-field Goto Github PK

View Code? Open in Web Editor NEW

This project forked from peerlibrary/meteor-reactive-field

0.0 2.0 0.0 11 KB

Reactive field for Meteor

Home Page: https://atmospherejs.com/peerlibrary/reactive-field

License: BSD 3-Clause "New" or "Revised" License

JavaScript 61.45% CoffeeScript 37.20% HTML 1.35%

meteor-reactive-field's Introduction

Reactive field for Meteor

Reactive field for Meteor provides an alternative syntax for ReactiveVar:

var field = new ReactiveField('initialValue');
console.log(field()); // prints 'initialValue' string
field('newValue');
console.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:

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

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

// You can also pass it as a third argument when using custom equality function
var field = new ReactiveField('initialValue', true);
console.log(field()); // prints 'initialValue' string
field('newValue');
console.log(field()); // prints 'newValue' string
console.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

meteor 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

  • reactive-var โ€“ the official Meteor reactive variable implementation which this package just wraps, so the functionality is the same, just syntax how to use it is different
  • meteor-variable โ€“ an obsolete package offering similar syntax to this package

meteor-reactive-field's People

Contributors

mitar avatar

Watchers

James Cloos avatar  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.