Skip to content

Instantly share code, notes, and snippets.

@toranb
Last active August 31, 2017 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toranb/47a9f3254808ad4a1e093c71393a1016 to your computer and use it in GitHub Desktop.
Save toranb/47a9f3254808ad4a1e093c71393a1016 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
import hbs from 'htmlbars-inline-precompile';
import { connect } from 'ember-redux';
const stateToComputed = state => {
return {
number: state.saga.number,
loading: state.saga.loading
};
};
const dispatchToActions = dispatch => {
return {
demo: () => dispatch({type: 'SAGA_ACTION_ASYNC', data: 'look ma generators!'})
};
};
const SagaComponent = Ember.Component.extend({
layout: hbs`
<button onclick={{action "demo"}}>Dispatch Saga!</button>
{{yield number loading}}
`
});
export default connect(stateToComputed, dispatchToActions)(SagaComponent);
import createSagaMiddleWare from 'redux-saga';
import root from '../sagas/index';
const sagaMiddleware = createSagaMiddleWare();
const setup = () => {
sagaMiddleware.run(root);
};
export default {
middleware: [sagaMiddleware],
setup: setup
};
import { combineReducers } from 'redux';
var initialState = {
number: 0,
loading: false
}
var saga = ((state, action) => {
if(action.type === 'LOADING') {
return Object.assign({}, state, {loading: true});
}
if(action.type === 'SAGA_ACTION') {
var number = state.number + 1;
return Object.assign({}, state, {number: number, loading: false});
}
return state || initialState;
});
export default combineReducers({
saga
});
import { delay } from 'redux-saga';
import { call, put, takeLatest } from 'redux-saga/effects';
function* demoAsync(action) {
console.log(action.data);
yield put({type: 'LOADING'});
yield call(delay, 1000);
yield put({type: 'SAGA_ACTION'});
}
export function* demo() {
yield takeLatest('SAGA_ACTION_ASYNC', demoAsync);
}
import { fork } from 'redux-saga/effects';
import { demo } from './example';
export default function* root() {
yield [
fork(demo)
];
}
{{#dispatch-saga as |number loading|}}
<h1>redux saga run {{number}}x</h1>
{{#if loading}}
loading ...
{{/if}}
{{/dispatch-saga}}
<p>Look at the `sagas/example.js` file to see how you can write async code with redux-saga. To configure redux-saga simply add the file you see here under `middleware/index.js`</p>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-redux": "3.0.0-beta.1",
"ember-redux-saga-shim": "1.1.0",
"ember-maybe-import-regenerator": "0.1.6"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment