Coder Social home page Coder Social logo

provider-behavior's Introduction

provider-behavior

Behavior to provide data for components. You can use this Behavior for polymer elements which manage data from API, automatically your data is stored in a closure for state control of your application. Based on redux, but only using the mono-state pattern (Singleton with sugar!).

Behavior API

method Params Description
get() Gets data from iron-ajax. You can add auto property in provider-element to get data and render it automatically
set(data,reflect) ArrayObject, Boolean Updates store with the new data, reflect true to apply updating for all same providers.
clearStore() Clears store, only removes data associated to its tagName (eg. all 'PROVIDER-ELEMENT')

How to use

Data binding from a declarative provider

<provider-element auto data="{{dataFromProvider}}"></provider-element>
<ol>
  <template is="dom-repeat" items="[[dataFromProvider]]">
    <li>[[item.value]]</li>
  </template>
</ol>

provider-element is using the provider-behavior, it automatically gets data from the iron-ajax element and sets result in its data property.

Using a provider method from an external element and reflecting the updated data

<dom-module id="provider-element">
  <template>
    <iron-ajax
      id="api"
      url="assets/data.json"
      handle-as="json">
    </iron-ajax>
  </template>

  <script>
    Polymer({
      is: "provider-element",

      behaviors: [Polymer.ProviderBehavior],

      add: function (newItem, reflect) {
        this.set(this.data.concat([newItem]), reflect || false);
      }
    });
  </script>
</dom-module>

You can set data and update all providers which are using the same data in the store. If you want to get that, only pass a second param to true as value.

<dom-module id="smart-component">
  <template>
    <provider-element id="provider" auto data="{{dataFromProvider}}"></provider-element>
    <dumb-component collection="[[dataFromProvider]]" on-add="addElement" on-add-reflect="addAndReflect"></dumb-component>
  </template>

  <script>
    Polymer({
      is: "smart-component",

      addElement: function (e) {
        this.$.provider.add(e.detail);
      },

      addAndReflect: function (e) {
        this.$.provider.add(e.detail, true);
      }
    });
  </script>
</dom-module>

provider-element contains an add method to add new data in the store and reflects this change to all providers which are using this data.

<dom-module id="dumb-component">
  <template>
    <ol>
      <template is="dom-repeat" items="[[collection]]">
        <li>[[item.value]]</li>
      </template>
    </ol>
    <button type="button" name="button" on-click="add">Add</button>
    <button type="button" name="button" on-click="addAndReflect">Add and reflect</button>
  </template>

  <script>
    Polymer({
      is: "dumb-component",

      properties: {
        collection: {
          type: Array
        }
      },

      add: function() {
        this.fire('add', {
          id: Math.random(),
          value: 'New item collection'
        });
      },

      addAndReflect: function() {
        this.fire('add-reflect', {
          id: Math.random(),
          value: 'New item collection'
        });
      }
    });
  </script>
</dom-module>

dumb-component only fires events which are managed by the smart-component

Try the provider-behavior demo

First, make sure you have the Polymer CLI installed. Then run polymer serve -o to serve your application locally.

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.