Skip to content

Instantly share code, notes, and snippets.

@toranb
Last active September 13, 2018 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save toranb/c6bebdbe5964c89598bea2761197e232 to your computer and use it in GitHub Desktop.
Save toranb/c6bebdbe5964c89598bea2761197e232 to your computer and use it in GitHub Desktop.
New Twiddle
export const updateItem = (id, name) => dispatch => dispatch({type: 'UPDATE_ITEM', id, name});
export const toggleEdit = (id, value) => dispatch => dispatch({type: 'TOGGLE_EDIT', id, value});
import Ember from 'ember';
export default Ember.Component.extend({
didReceiveAttrs() {
this._super(...arguments);
const itemName = this.get('item.name');
this.set('itemName', itemName);
}
});
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'li'
});
import Ember from 'ember';
import { connect } from 'ember-redux';
import { toggleEdit, updateItem } from '../actions/items';
const ListComponent = Ember.Component.extend({
tagName: 'ul'
});
const stateToComputed = state => ({
items: state.items.all,
editing: state.items.editing
});
const dispatchToActions = {
toggleEdit,
updateItem
};
export default connect(stateToComputed, dispatchToActions)(ListComponent);
import Ember from 'ember';
export default Ember.Helper.helper(function(params) {
return params[0] === params[1];
});
import { combineReducers } from 'redux'
import items from './items'
const rootReducer = combineReducers({
items
})
export default rootReducer
import mapValues from 'lodash/mapValues';
import defaults from 'lodash/defaults';
const initialState = {
editing: null,
all: {
1: {
id: 1,
name: 'One'
},
2: {
id: 2,
name: 'Two'
},
3: {
id: 3,
name: 'Three'
}
}
};
export default function items(state, action) {
switch (action.type) {
case 'UPDATE_ITEM': {
let items = mapValues(state.all, item => {
return item.id === action.id ? defaults({
name: action.name
}, item) : item;
});
return Object.assign({}, state, { all: items });
}
case 'TOGGLE_EDIT': {
let editing = !action.value ? null : action.id;
return Object.assign({}, state, { editing: editing });
}
default: {
return state || initialState;
}
}
}
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
ul { list-style-type: none; }
<h1>Master Detail Example</h1>
<br>
{{#ui-list as |items editing updateItem toggleEdit|}}
{{#each-in items as |key item|}}
{{ui-item item=item editing=editing updateItem=(action updateItem item.id) toggleEdit=(action toggleEdit item.id)}}
{{/each-in}}
{{/ui-list}}
<form {{action updateItem itemName on="submit"}}>
<input type="text" value={{itemName}} oninput={{action (mut itemName) value="target.value"}}>
<input type="submit" value="submit" />
</form>
<span>{{item.name}}</span>
{{#if (is-equal item.id editing)}}
<button onclick={{action toggleEdit false}}>done</button>
{{ui-form item=item updateItem=updateItem}}
{{else}}
<button onclick={{action toggleEdit true}}>edit</button>
{{/if}}
{{yield items editing (action "updateItem") (action "toggleEdit")}}
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-redux": "4.0.0",
"ember-lodash-es-shim": "1.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment