npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

store-x

v1.0.6

Published

Universal Flux Stores

Downloads

4

Readme

store-x

Universal Flux Stores

Build Status

Simple yet powerful Flux stores implementation. It's meant to work with any JS framework or library, and on the server as well.

Install

You can get it from NPM:

npm install store-x --save

Or from Bower:

bower install store-x --save

Usage

You can use it with any module system

  • Globals:
var myStore = window.storex({
  method: function () {
    return [];
  }
});
  • ES6 / ES2015
import storex from 'store-x';

var myStore = storex({
  method () {
    return [];
  }
});

export default myStore;
  • CommonJS
var storex = require('store-x');

var myStore = storex({
  method: function () {
    return [];
  }
});

module.exports = myStore;
  • AMD
define(['store-x'], function (storex) {

  var myStore = storex({
    method: function () {
      return [];
    }
  });

  return myStore;
});

API

The store-x's API is very simple. You just need to give it an object with methods. Then, you'll be able to call those methods, and listen to events when the state changes.

There's a method to initialize the store's state, which is the init method. This one is going to be executed automatically as soon as the store is created.

// in this case we initialize the
// store's state with an empty array.
var myStore = store({
  init: function () {
    return [];
  }
});

Store methods can return anything. The returned value will replace the store's state. You can even return promises and store-x will replace the state with the result of the promise. It works not only with jQuery promises, but with any Promises/A+ implementation.

// in this case we initialize the
// store's state with the result of
// calling an API with jQuery.
var myStore = store({
  init: function () {
    return $.ajax({
      url: 'https://api.mysite.com/people',
      method: 'GET'
    });
  }
});

You can listen to events which will be fired whenever the store's state changes.

var myStore = store({
  init: function () {
    return $.ajax({
      url: 'https://api.mysite.com/people',
      method: 'GET'
    });
  },
  create: function (person) {
    var state = this.state;
    return $.ajax({
      url: 'https://api.mysite.com/people',
      method: 'POST',
      data: person
    }).then(function (person) {
      state.push(person);
      return state;
    });
  }
});

// in this case we listen to all events.
// notice that the handler will be called
// as soon as it's attached to the event if
// there aren't any pending promises.
myStore.listen(function (err, state) {
  if (err) {
    // handle error
    return;
  }
  console.log('this is the current state', state);
});

// you can also listen to specific events.
myStore.listen('init', function (err, state) {
  if (err) {
    // handle error
    return;
  }
  console.log('the store was just initialized');
});

myStore.listen('create', function (err, state) {
  if (err) {
    // handle error
    return;
  }
  console.log('a person was just created');
});

// here's how we can invoke a method.
myStore.create({
  first_name: 'John',
  last_name: 'Doe'
});

Using it with Angular.js

You need to include: store-x/dist/store-x.angular.js or store-x/dist/store-x.angular.min.js

var myApp = angular.module('myApp', ['storex']);

myApp.factory('PeopleStore', ['storex', '$http', function (storex, $http) {
  var api = 'https://api.mysite.com/people';
  return storex({
    init: function () {
      return $http.get(api).then(function (response) {
        return response.data;
      });
    },
    create: function (person) {
      var state = this.state;
      return $http.post(api, person).then(function (response) {
        state.push(response.data);
        return state;
      });
    }
  });
}]);

myApp.controller('PeopleController', ['$scope', 'PeopleStore', function ($scope, PeopleStore) {
  $scope.people = [];
  $scope.form = {};

  PeopleStore.listen(function (err, state) {
    if (err) {
      // handle the error
      return;
    }
    $scope.people = state;
  });

  $scope.create = function create() {
    var person = {
      first_name: $scope.form.first_name,
      last_name: $scope.form.last_name
    };
    PeopleStore.create(person);
    $scope.form = {};
  }
}]);

Using it with jQuery

You need to include: store-x/dist/store-x.js or store-x/dist/store-x.min.js

$(function () {

  var api = 'https://api.mysite.com/people';

  // Create a store
  var peopleStore = storex({
    init: function () {
      return $.get(api);
    },
    create: function (person) {
      var state = this.state;
      return $.post(api, person).then(function (person) {
        state.push(person);
        return state;
      });
    }
  });

  // Listen to all events
  peopleStore.listen(function (err, state) {
    if (err) {
      console.error('Danger danger!');
    }
    console.log(state);
  });

  // Listen to the `init` event
  peopleStore.listen('init', function (err, state) {
    if (err) {
      console.error('Danger danger!');
    }
    console.log(state);
  });

  // Listen to the `create` event
  peopleStore.listen('create', function (err, state) {
    if (err) {
      console.error('Danger danger!');
    }
    console.log(state);
  });

  // Execute an action
  $('.btn').click(function (event) {
    peopleStore.create({
      first_name: 'John',
      last_name: 'Doe'
    });
  });

});