• Stars
    star
    225
  • Rank 173,670 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ember State Services

Build Status Code Climate Ember Observer Score npm version

This addon introduces a state management pattern for your ambitious applications.

State management is one of the most complex aspects of large application design and when done wrong often leads to bugs and errors. EmberJS contains 2 high-level avenues for storing state: controllers (long-term state) and components (short-term state). Controllers are singletons and any state you set on them will stay there until your application is reloaded or you override the previous value. Components on the other hand are created and destroyed as they enter/leave the DOM and any state that is set on them will be removed/reset each time they are recreated. As you build more complex applications you will find yourself needing a way to have some sort of middle ground solution. Something that has properties of both long-term state and short-term state. This is what ember-state-services sets out to provide.

An example could be a master/detail experience where the detail view is a component which allows editing of content. It would be unfortunate if navigating would lose un-saved changes (short-term state); it would also be unfortunate if the state between the edit components were to leak between each other (long-term state). Instead, the addon issues a unique state per reference key, which keeps management safe and easy.

Installation

ember install ember-state-services

Setup

State file

/*
 * First create a state file that returns an object within app/states/<STATE_NAME>.js
 */
import Ember from 'ember';

export default Ember.Object.extend();

Usage

Component

import Ember from 'ember';
import stateFor from 'ember-state-services/state-for';

export default Ember.Component.extend({
  tagName: 'form',

  /*
  * stateFor returns a computed property that provides a given
  * state object based on the 'email' property. Whenever email
  * changes a new state object will be returned.
  *
  * This allows us to create private state, accessible only to those
  * with access to the given model (email in this case) and the state
  * bucket (<STATE_NAME> in this case).
  */
  data: stateFor('<STATE_NAME>', 'email'),

  actions: {
    submitForm() {
      // apply changes to the email model
      this.get('email').setProperties(this.get('data'));
    }
  }
});

Template

<label>Subject: {{input value=data.subject}}</label><br>
<label>from:   {{input value=data.from}}</label><br>
<label>body:   {{textarea value=data.body}}</label><br>
<button {{action 'submitForm'}}>Submit Form</button>

Advanced

Setting initial state

import Ember from 'ember';

const MyStateObject = Ember.Object.extend();

MyStateObject.reopenClass({
  initialState(instance) {
    return {
      foo: 'bar',
      hello: 'world'
    };
  }
});

export default MyStateObject;

Using ember-buffered-proxy

import BufferedProxy from 'ember-buffered-proxy/proxy';

const MyStateObject = BufferedProxy.extend();

MyStateObject.reopenClass({
  initialState() {
    return {
      content: {
        foo: 'bar',
        hello: 'world'
      }
    };
  }
});

export default MyStateObject;

Learn more about buffered proxy: https://github.com/yapplabs/ember-buffered-proxy

Example Demo

git clone [email protected]:stefanpenner/ember-state-services.git
cd ember-state-services
npm i; bower i
ember server

Then visit the demo app: http://localhost:4200 in your browser

More Repositories

1

es6-promise

A polyfill for ES6-style Promises
JavaScript
7,303
star
2

ember-app-kit

deprecated: see https://github.com/stefanpenner/ember-cli
JavaScript
1,020
star
3

broccoli-concat-analyser

HTML
125
star
4

ember-jobs

JavaScript
72
star
5

ember-browsery-stats

JavaScript
62
star
6

broccoli-stew

JavaScript
60
star
7

ember-console

experimental node console for your ember app (fastboot capable ember apps only)
JavaScript
38
star
8

get-caller-file

TypeScript
30
star
9

resolve-package-path

a special purpose fast memoizing way to resolve a node modules package.json
TypeScript
29
star
10

ember-app-kit-todos

JavaScript
28
star
11

ember-improved-cp

JavaScript
28
star
12

ember-cli-ember-fire

JavaScript
27
star
13

fs-tree-diff

TypeScript
24
star
14

async-disk-cache

JavaScript
22
star
15

es3-safe-recast

JavaScript
22
star
16

ember-runloop-utils

JavaScript
15
star
17

broccoli-swc

Experimental SWC compiler for broccoli.
JavaScript
14
star
18

dotfiles

Shell
13
star
19

detect-indent-rs

Rust
13
star
20

template-ts

GH Template your way to friction free typescript library bliss.
TypeScript
13
star
21

node-fixturify-project

TypeScript
12
star
22

ember-strict-resolver

JavaScript
10
star
23

sync-disk-cache

JavaScript
9
star
24

rsvp-party

JavaScript
9
star
25

pleasant-progress

JavaScript
9
star
26

ember-cli-auto-register-helpers

JavaScript
9
star
27

ember-redirect-to

JavaScript
9
star
28

layout-bin-packer

JavaScript
8
star
29

do-you-even-bench

quick and dirty benchmark.js wrapper for us simple folk
JavaScript
8
star
30

mocha-esm

Quick trick to get to use mocha + esm/.mjs
JavaScript
8
star
31

tree-sync

JavaScript
7
star
32

hash-for-dep

JavaScript
7
star
33

state_machine.js

simple JS statemachine
JavaScript
7
star
34

petal

JavaScript
7
star
35

promise_benchmarks

micro benchmarks
JavaScript
6
star
36

heapsnapshot

JavaScript
6
star
37

node-require-timings

quick lib to quickly discover which modules are costly to require
JavaScript
6
star
38

perf-stress

JavaScript
6
star
39

lib-kit

JavaScript
6
star
40

alt-yarn-why

yarn why is cool, but sometimes it doesn't show what i want.. This helps.
JavaScript
6
star
41

d-d

JavaScript
5
star
42

graphql-fragment-import

JavaScript
5
star
43

ember-cli-progress

the long lost ember-cli progress bar
JavaScript
5
star
44

ember-cli-jsbin

JavaScript
5
star
45

ember-test-helpers

moved to -> https://github.com/rpflorence/ember-qunit
JavaScript
5
star
46

ember-performance

JavaScript
5
star
47

ember-inspector

you are likely looking for ember-extension
Ruby
5
star
48

ember-weak-ref

JavaScript
4
star
49

ast-equality

JavaScript
4
star
50

broccoli-plugin-kit

How to get started writing broccoli plugins
JavaScript
4
star
51

promise.hash.helper

Inspired by Promise.all but rather then consuming an array it takes an object as input.
TypeScript
4
star
52

pp-colour

Colourized PP
Ruby
4
star
53

terminal-color

TODO: one-line summary of your gem
Ruby
3
star
54

eslint-ast

Mono-repo for various eslint custom language parsers / rules etc.
JavaScript
3
star
55

random

JavaScript
3
star
56

simply_paginate

TODO: one-line summary of your gem
Ruby
3
star
57

ember-tron

JavaScript
3
star
58

flash.js

yet another JavaScript flash loading library
3
star
59

basic-transpiler-benchmarks

JavaScript
3
star
60

rebinder

Ruby
3
star
61

async-promise-queue

A wrapper around the async module, that provides an improved promise queue.
JavaScript
3
star
62

matcher-collection

TypeScript
3
star
63

ember-es6-object

object that pretends to be a minimal ember object, but is just a plain es6 class
JavaScript
3
star
64

sweet-array-slice

sweet.js macro for array splice, specifically to deal with argument splicing deopt.
JavaScript
3
star
65

all-addons

Shell
3
star
66

blog

2
star
67

ember-cli-update-reporter

JavaScript
2
star
68

hamachi

OSX menu item for Hamachi
Ruby
2
star
69

53cr.com

JavaScript
2
star
70

table_to_model

Using ActiveRecord, generate all the models from a database.
Ruby
2
star
71

rubygems_dump

TODO: one-line summary of your gem
Ruby
2
star
72

___ember-data-fixture-adapter-example

JavaScript
2
star
73

sass_mixins

some useful sass mixins
2
star
74

multiparameter_attribute_bug

Ruby
2
star
75

my_scaffold

TODO
Ruby
2
star
76

ckeditor

TODO: one-line summary of your gem
Ruby
2
star
77

ember-cli-addon-aware-resolver

JavaScript
2
star
78

broccoli-templater

JavaScript
2
star
79

find_p_dupes

Ruby
2
star
80

perf-scratch

JavaScript
2
star
81

mjs-dirname

JavaScript
2
star
82

ruby-git

TODO: one-line summary of your gem
Ruby
2
star
83

Intro-to-debugging-ruby

Winnipegrb intro to debugging quick-talk
Ruby
2
star
84

active_model_humanized_attributes

TODO: one-line summary of your gem
Ruby
2
star
85

host_app

rails 2.3rc1 application demonstrating an engine layout issue.
Ruby
2
star
86

vhosterous

TODO: one-line summary of your gem
Ruby
2
star
87

vim

Shell
2
star
88

fovea

Ruby
2
star
89

___my-cool-app

JavaScript
2
star
90

association_collection_create_bug

collect create bug
Ruby
2
star
91

blprnt-node

JavaScript
2
star
92

open_flash_chart_2

TODO
Ruby
2
star
93

dotbars

JavaScript
2
star
94

referrer.js

Provides some simple helpers to extract useful information like keywords from the referrer url
JavaScript
2
star
95

logical.rb

Ruby
2
star
96

mr-dep-walk

JavaScript
2
star
97

date_range.js

A lightweight library that provides tools for dealing with date ranges
JavaScript
2
star
98

fs-thing

JavaScript
2
star
99

ember-install-gist

JavaScript
1
star
100

ember-promise

JavaScript
1
star