Skip to content

Instantly share code, notes, and snippets.

@mwpastore
Created September 13, 2017 06:48
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 mwpastore/e27f0a4e05e714d6ef4368b002c532a4 to your computer and use it in GitHub Desktop.
Save mwpastore/e27f0a4e05e714d6ef4368b002c532a4 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
store: Ember.inject.service(),
count: 0,
foo: null,
init() {
this._super(...arguments);
this.set('foo', this.get('store').createRecord('foo'));
},
_manager: Ember.computed('foo.bars.@each.isBlank', function() {
this.incrementProperty('count');
Ember.run.scheduleOnce('afterRender', this, 'update');
}),
update() {
const
bars = this.get('foo.bars'),
lastIndex = bars.get('length') - 1,
blankBars = bars.filter((bar, index) => {
return index !== lastIndex && bar.get('isBlank');
});
blankBars.forEach((blankBar) => {
// first disassociate it to update the UI
blankBar.set('foo', null);
// garbage collect
Ember.run.later(blankBar, 'rollbackAttributes');
});
if (!bars.get('lastObject.isBlank')) {
// always ensure a blank record at the end
bars.createRecord();
}
}
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
qux: attr('string'),
isBlank: Ember.computed('qux', function() {
return Ember.isBlank(this.get('qux'));
}).readOnly()
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
bars: hasMany({ async: false })
});
{{_manager}}
<div>Count: {{count}}</div>
{{#each foo.bars as |bar|}}
<div>{{input value=bar.qux}}</div>
{{/each}}
{
"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-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment