All Projects → ivanrod → Polobx

ivanrod / Polobx

Licence: mit
A state manager for Polymer based in MobX

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Polobx

Favesound Mobx
🎶 A SoundCloud Client in React + MobX running in production. Live Demo and Source Code to explore React + MobX. Refactored from favesound-redux
Stars: ✭ 532 (+2116.67%)
Mutual labels:  mobx
Wewechat
💬 Unofficial WeChat client built with React, MobX and Electron.
Stars: ✭ 5,618 (+23308.33%)
Mutual labels:  mobx
Weapp Bmscore
a weapp to count badminton game score
Stars: ✭ 17 (-29.17%)
Mutual labels:  mobx
Mobx
Simple, scalable state management.
Stars: ✭ 24,636 (+102550%)
Mutual labels:  mobx
Surmon.me.native
📱 My blog app, powered by react-native
Stars: ✭ 579 (+2312.5%)
Mutual labels:  mobx
Mobx State Tree
Full-featured reactive state management without the boilerplate
Stars: ✭ 6,317 (+26220.83%)
Mutual labels:  mobx
Web Series
📚 现代 Web 开发语法基础与工程实践,涵盖 Web 开发基础、前端工程化、应用架构、性能与体验优化、混合开发、React 实践、Vue 实践、WebAssembly 等多方面。
Stars: ✭ 666 (+2675%)
Mutual labels:  mobx
Figmiro Plugin
Figma Integration with Miro (Plugin)
Stars: ✭ 23 (-4.17%)
Mutual labels:  mobx
Ignite Bowser
Bowser is now re-integrated into Ignite CLI! Head to https://github.com/infinitered/ignite to check it out.
Stars: ✭ 586 (+2341.67%)
Mutual labels:  mobx
Gdg.es
The GDG Spain official website
Stars: ✭ 16 (-33.33%)
Mutual labels:  polymer
React Mobx React Router4 Boilerplate
React, React-Router 4, MobX and Webpack 2-boilerplate with async routes.
Stars: ✭ 566 (+2258.33%)
Mutual labels:  mobx
Web Component Tester
Moved to Polymer/tools monorepo
Stars: ✭ 571 (+2279.17%)
Mutual labels:  polymer
Snippetstore
🎉 A snippet management app for developers 🚀
Stars: ✭ 762 (+3075%)
Mutual labels:  mobx
Polymer
Our original Web Component library.
Stars: ✭ 21,723 (+90412.5%)
Mutual labels:  polymer
Paper Timezone
Polymer based timezone selection component
Stars: ✭ 19 (-20.83%)
Mutual labels:  polymer
Mobx Persist
persist mobx stores
Stars: ✭ 525 (+2087.5%)
Mutual labels:  mobx
Shiba
Rich markdown live preview app with linter
Stars: ✭ 691 (+2779.17%)
Mutual labels:  polymer
Openui5 Mobx Model
ui5 bindings for mobx
Stars: ✭ 23 (-4.17%)
Mutual labels:  mobx
Graphql Mst
Convert GraphQL to mobx-state-tree models
Stars: ✭ 22 (-8.33%)
Mutual labels:  mobx
Lancie Frontend
Website for Area FiftyLAN
Stars: ✭ 5 (-79.17%)
Mutual labels:  polymer

travis CI Published on webcomponents.org

Polobx

State manager for Polymer based on MobX.

Uses the Monostate Pattern such that any instance with the behavior will share the same state.

Inspired by tur-nr/polymer-redux & flux.

You can see an example app here.

See the docs.

Table of Contents

  1. Install
  2. Usage
  1. Polobx API
  2. License

Install

With bower:

$ bower install --save polobx

Usage

Use the createPolobxBehavior factory method to create the behavior. This will give the behavior access to the bindings:

my-state.html

<link rel="import" href="../bower_components/polobx/polobx.html">

<script type="text/javascript">
var myStore = {

  model: {
    foo: 'bar'
  },

  actions: {
    changeFoo: function(newFoo) {
      this.model.foo = newFoo;
    }
  }
};

var myOtherStore = {

  model: {
    counter: 0,
    xxx: {
      xx: 'x'
    }
  },

  actions: {
    addOne: function() {
      this.model.counter++;
    }
  }
};

window.PolobxBehavior = createPolobxBehavior(
  {
    myStore: myStore,
    myOtherStore: myOtherStore
  }
);
</script>

Binding Properties

With your PolobxBehavior you can bind your state to properties of your elements.

Use statePath field in your property to define the store and path you want to bind to it:

my-view.html

<link rel="import" href="my-state.html">
<dom-module id="my-view">
  <template>
    ...
    <p>My element var: [[myVar]]</p>
    ...
  </template>
  <script>
    Polymer({
      is: 'my-view',

      behaviors: [PolobxBehavior],

      properties: {

        myVar: {
          type: String,
          statePath: {
            store: 'myStore',
            path: 'foo'
          }
        },

        myOtherVar: {
          type: String,
          statePath: {
            store: 'myOtherStore',
            path: 'xxx.xx'
          }
        }

      }
    });
  </script>
</dom-module>

State Observers

Define a stateObservers field in your component with a list of observers of your state:

<link rel="import" href="my-state.html">
<dom-module id="my-view">
  <template>
    ...
    <p>My colors counter: [[myColorsCounter]]</p>
    <p>My foo: [[myFoo]]</p>
    ...
  </template>
  <script>
    Polymer({
      is: 'my-view',

      behaviors: [PolobxBehavior],

      stateObservers: [
        // Store path observer
        {
          store: 'myStore',
          path: 'colors',
          observer: function(colors) {
            if (colors.length > 4) {
              this.set('myColorsCounter', 'We have more than 4.')
            }
          }
        },

        // Store observer
        {
          store: 'myStore',
          observer: function(state) {

            if (state.foo === 'bar') {
              this.set('myFoo', 'My foo is actually bar.')
            }
          }
        }

      ],

      properties: {

        myColorsCounter: String,

        myFoo: String

      }
    });
  </script>
</dom-module>

Dispatch actions

Using PolobxBehavior you can use dispatch() inside your element to dispatch a defined action of your store:

my-other-view.html

<link rel="import" href="my-state.html">
<dom-module id="my-other-view">
  <template>
    ...
    <button on-click="changeOtherViewVar">X</button>
    ...
  </template>
  <script>
    Polymer({
      is: 'my-other-view',

      behaviors: [PolobxBehavior],

      changeOtherViewVar: function() {
        this.dispatch({
          store: 'myStore',
          action: 'changeFoo',
          payload: 'OtherBar'
        })
      }

    });
  </script>
</dom-module>

Polobx API

Behavior API

dispatch({store:string, action:string, payload:any})

Dispatch an action to a defined store.

Returns the action result.

Example:

this.dispatch({
          store: 'myStore',
          action: 'changeFoo',
          payload: 'OtherBar'
        });
// -> <Action Result>

getStateProperty(store:string, path:string)

Get a field/property of the selected store.

Returned value is just a copy of the store property, you can only modify it when dispatching an action.

Example:

this.getStateProperty('myStore', 'foo');
// -> 'bar'

Test

Run:

npm run test

License

MIT © ivanrod.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].