-
-
Save zigotica/ebc9f115f2ce82ac91661aef4f717a6d to your computer and use it in GitHub Desktop.
Repeatable Fields
This file contains hidden or 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.Component.extend({ | |
init() { | |
this._super(...arguments); | |
this.set('components', Ember.A([Ember.Object.create({value: ''})])); | |
}, | |
didReceiveAttrs() { | |
this._super(...arguments); | |
this.set('maxRepeats', Math.max(1, parseInt(this.get('max-repeats'), 10)) || 5); | |
this.set('minRepeats', Math.max(1, parseInt(this.get('min-repeats'), 10)) || 2); | |
}, | |
addElementIfNeeded() { | |
console.log(this.get('components.length'), this.get('maxRepeats'), this.get('components.lastObject.value')); | |
if (this.get('components.length') < this.get('maxRepeats') && | |
Ember.isPresent(this.get('components.lastObject.value'))) { | |
this.get('components').pushObject(Ember.Object.create({value:''})); | |
} | |
}, | |
actions: { | |
removeRepetition(idx) { | |
this.get('components').removeAt(idx, 1); | |
this.addElementIfNeeded(); | |
}, | |
onInput(idx, value) { | |
}, | |
onChange(idx, value) { | |
this.get('components') | |
.objectAt(idx) | |
.set('value', value); | |
this.get('onchange')(this.get('components'), idx, value); | |
this.addElementIfNeeded(); | |
} | |
} | |
}); |
This file contains hidden or 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.Component.extend({ | |
init() { | |
this._super(...arguments); | |
this.set('value', ''); | |
}, | |
actions: { | |
onInput(value) { | |
this.get('oninput')(value); | |
}, | |
onChange(value) { | |
this.get('onchange')(value); | |
} | |
} | |
}); |
This file contains hidden or 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({ | |
appName: 'Ember Twiddle', | |
actions: { | |
repeatableFieldChanged() { } | |
} | |
}); |
This file contains hidden or 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 function arr(params=[]/*, hash*/) { | |
return params.slice(0); | |
} | |
export default Ember.Helper.helper(arr); |
This file contains hidden or 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 function isDeletable([value, idx]/*, hash*/) { | |
return Ember.isPresent(value) && idx > 0; | |
} | |
export default Ember.Helper.helper(isDeletable); |
This file contains hidden or 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 function isExtraField([idx]/*, hash*/) { | |
return idx > 0; | |
} | |
export default Ember.Helper.helper(isExtraField); |
This file contains hidden or 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 function isMainField([idx]/*, hash*/) { | |
return idx === 0; | |
} | |
export default Ember.Helper.helper(isMainField); |
This file contains hidden or 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
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
input:required { | |
border-color: red; | |
} |
This file contains hidden or 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.10.5", | |
"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.9.0-beta.4", | |
"ember-template-compiler": "2.9.0-beta.4" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment