Last active
August 1, 2018 13:54
-
-
Save alexmiddeleer/92d991dfb1bd7fbe6a84a2bed104d29a to your computer and use it in GitHub Desktop.
power select with create
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 { later } from '@ember/runloop'; | |
import { Promise } from 'rsvp'; | |
import Controller from '@ember/controller'; | |
const countries = [ | |
{ name: 'United States', code: 'US', population: 321853000 }, | |
{ name: 'Spain', code: 'ES', population: 46439864 }, | |
{ name: 'Portugal', code: 'PT', population: 10374822 }, | |
{ name: 'Russia', code: 'RU', population: 146588880 }, | |
{ name: 'Latvia', code: 'LV', population: 1978300 }, | |
{ name: 'Brazil', code: 'BR', population: 204921000 }, | |
{ name: 'United Kingdom', code: 'GB', population: 64596752 }, | |
]; | |
export default Controller.extend({ | |
countries, | |
slowPromise: null, | |
init() { | |
this._super(...arguments); | |
this.set('slowPromise', this.createSlowPromise()); | |
this.set('selectedCountries', []); | |
}, | |
// Actions | |
actions: { | |
createCountry(countryName) { | |
let newCountry = { name: countryName, code: 'XX', population: 'unknown' }; | |
this.get('countries').pushObject(newCountry); | |
this.set('selectedCountry', newCountry); | |
this.get('selectedCountries').push(newCountry); | |
}, | |
searchCountries(term) { | |
return new Promise((resolve, reject) => { | |
this.createSlowPromise(2000).then((countries) => { | |
resolve(countries.filter((country) => { | |
return country.name.toLowerCase().match(term.toLowerCase()); | |
})); | |
}, reject); | |
}); | |
}, | |
hideCreateOptionOnSameName(term) { | |
let existingOption = this.get('countries').findBy('name', term); | |
return !existingOption; | |
}, | |
}, | |
// Methods | |
capitalizeSuggestion(term) { | |
return `Hey! Perhaps you want to create ${term.toUpperCase()}??`; | |
}, | |
createSlowPromise(time = 5000) { | |
return new Promise(function(resolve) { | |
later(() => resolve(countries), time); | |
}); | |
}, | |
}); |
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 DS from 'ember-data'; | |
const { attr, Model } = DS; | |
export default Model.extend({ | |
name: attr('string'), | |
code: attr('string'), | |
population: attr('number') | |
}); |
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.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-data": "3.2.0", | |
"ember-power-select-with-create": "0.6.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment