Created
September 13, 2017 06:48
-
-
Save mwpastore/e27f0a4e05e714d6ef4368b002c532a4 to your computer and use it in GitHub Desktop.
New Twiddle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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