项目作者: ember-redux

项目描述 :
Predictable state management for ember apps
高级语言: JavaScript
项目地址: git://github.com/ember-redux/ember-redux.git
创建时间: 2016-03-26T00:04:28Z
项目社区:https://github.com/ember-redux/ember-redux

开源协议:MIT License

下载


Ember Redux

Travis Code Climate Score Downloads npm package

Predictable state management for ember apps

Installation

ember redux requires ember v3.6+ and node >= 8. If you need support for an older version of ember use the v5 release

  1. ember install ember-redux

Documentation and Examples

https://ember-redux.com

Demo

Counter
https://ember-twiddle.com/5bee7478e4216abe49f1c0a439bae352

TodoMVC
https://ember-twiddle.com/4bb9c326a7e54c739b1f5a5023ccc805

Usage

Container Component

  1. import Component from '@ember/component';
  2. import hbs from 'htmlbars-inline-precompile';
  3. import { connect } from 'ember-redux';
  4. import getUsersByAccountId from '../reducers';
  5. import fetch from 'fetch';
  6. const stateToComputed = (state, attrs) => ({
  7. users: getUsersByAccountId(state, attrs.accountId)
  8. });
  9. const dispatchToActions = (dispatch) => ({
  10. remove: (id) => fetch(`/api/users/${id}`, {method: 'DELETE'}).then(fetched => fetched.json()).then(response => dispatch({type: 'REMOVE_USER', id: id}))
  11. });
  12. const UserListComponent = Component.extend({
  13. layout: hbs`
  14. {{yield users (action "remove")}}
  15. `
  16. });
  17. export default connect(stateToComputed, dispatchToActions)(UserListComponent);

Presentation Component

  1. import Component from '@ember/component';
  2. import hbs from 'htmlbars-inline-precompile';
  3. const UserTableComponent = Component.extend({
  4. layout: hbs`
  5. {{#each users as |user|}}
  6. <div>{{user.name}}</div>
  7. <button onclick={{action remove user.id}}>remove</button>
  8. {{/each}}
  9. `
  10. });
  11. export default UserTableComponent;

Composition

  1. {{#user-list accountId=accountId as |users remove|}}
  2. {{user-table users=users remove=remove}}
  3. {{/user-list}}

Octane Support?

As of version 6 ember-redux now supports both ember component and glimmer component. One brief example of glimmer components and ember redux below.

  1. import Component from '@glimmer/component';
  2. import { action } from '@ember/object';
  3. import { connect } from 'ember-redux';
  4. import getUsersByAccountId from '../reducers';
  5. import fetch from 'fetch';
  6. const stateToComputed = (state, attrs) => ({
  7. users: getUsersByAccountId(state, attrs.accountId)
  8. });
  9. const dispatchToActions = (dispatch) => ({
  10. remove: (id) => fetch(`/api/users/${id}`, {method: 'DELETE'}).then(fetched => fetched.json()).then(response => dispatch({type: 'REMOVE_USER', id: id}))
  11. });
  12. class MyClazz extends Component {
  13. @action
  14. example() {
  15. this.actions.remove();
  16. }
  17. }
  18. export default connect(stateToComputed, dispatchToActions)(MyClazz);

How do I enable time travel debugging?

  1. Install the redux dev tools extension.

  2. Enjoy!

Running Tests

  1. yarn
  2. ember test

License

Copyright © 2019 Toran Billups https://toranbillups.com

Licensed under the MIT License