Giter Club home page Giter Club logo

blaze-local-state's Introduction

Blze Local State

It is a common pattern in Blaze UI code to rely on session variables:

Session.setDefault('counter', 0);

Template.hello.helpers({
  key: function () {
    return Session.get('counter');
  }
});

Template.hello.events({
  'click button': function () {
    Session.set('counter', Session.get('counter') + 1);
  }
});

There are several problems with this pattern:

  1. Relies on the global Session, could accidentally overwrite the variable elsewhere;
  2. The same variables are shared across all instances of the hello template, and often times we need to explicitly reset the state whenever the template is re-rendered.
  3. When there are multiple variables it becomes unecessarily verbose to define and create helpers for each of them.

By using local ReactiveVars instead of Session we can avoid issue 1, but issue 2 and 3 remains the same.

This package provides some syntax sugar to provide local reactive state to Blaze templates:

Template.hello.localState(function () {
  // this function is called when a template instance is
  // created to provide its initial local state.
  return {
    // a 'counter' helper will automatically be created.
    counter: 0
  }
});

Template.hello.events({
  'click button': function (event, template) {
    // template instances now have .get() and .set() methods
    // which are reactive
    template.set('counter', template.get('counter') + 1);
  }
});

blaze-local-state's People

Contributors

yyx990803 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

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