Code Monkey home page Code Monkey logo

ember-action-services's Introduction

ember-action-services

Action services contain immutable state and actions that mutate that state

๐Ÿ’ก If you have ideas for the direction of this addon, please open an issue. I'd love to hear your thoughts!

Installation

ember install ember-action-services

Usage

Action services let you share actions and state across multiple components and controller/template combos in a safe way.

An action service defines immutable state, which can only be modified by calling setState on that service. The developer then creates actions on that service, and modify the state using setState. Note that async actions work using async/await.

Setup Action Service

Create a new service using ember g service <name>. Then modify the created file to look something like this:

import { ActionService } from 'ember-action-services';

export default ActionService.extend({
  /*
    This is optional, but if not set the default state is an empty object.
    This could also be the place you bring back state from localStorage or something similar.
  */
  initState() {
    return {
      test: {
        name: 'hi'
      }
    };
  },
  
  actions: {
    duplicate() {
      /*
        `pluckState` is provided by ActionService and takes 1 or more keys as arguments, e.g. `this.pluckState('cloneUser', 'test')` will return  `{ cloneUser, test }` object.
      */
      let { cloneUser } = this.pluckState('cloneUser');

      cloneUser = cloneUser ? cloneUser + 1 : 1;

      /*
        `setState` is also provided by ActionService and takes an object that is merged with the existing state.
      */
      this.setState({ cloneUser });
    },
    
    setName() {
      this.setState({
        test: {
          name: 'good bye'
        }
      });
    }
  }
});

Not the use of ActionService rather then the default Ember.Service.

Using Action Service

To call actions on an action service, you would first inject that service where you need it. For example:

import Component from '@ember/component';
import { inject as service } from '@ember/service';

export default Component.extend({
  userStateService: service('user-state')
});

Then you can use that service in your template:

<button type='button' onclick={{action 'duplicate' target=userStateService}}>Duplicate User</button>

To access state data, you can do so via normal Ember conventions under the state path under that service, e.g.

User was cloned {{userStateService.state.cloneUser}} times.

Contributing

Installation

  • git clone <repository-url>
  • cd ember-action-services
  • npm install

Linting

  • npm run lint:js
  • npm run lint:js -- --fix

Running tests

  • ember test โ€“ Runs the test suite on the current Ember version
  • ember test --server โ€“ Runs the test suite in "watch mode"
  • ember try:each โ€“ Runs the test suite against multiple Ember versions

Running the dummy application

For more information on using ember-cli, visit https://ember-cli.com/.

License

This project is licensed under the MIT License.

ember-action-services's People

Contributors

ember-tomster avatar

Stargazers

Ilya Radchenko avatar

Watchers

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