Created
March 9, 2016 06:46
-
-
Save jonathansamines/214ac02a5f5da0cc2110 to your computer and use it in GitHub Desktop.
ES6 sample code
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.Object.extend({ | |
gtCountry: { code: 'GT', region: 'LATAM' }, | |
usCountry: { code: 'US', region: 'NA' }, | |
filterByRegion(region) { | |
// adiós al "arguments" objeto. No más conversiones a arrays! | |
const countries = Array.prototype.slice.call(arguments, 1); | |
const filtereCountries = countries.filter((country) => country.region === region); | |
// no más .apply sobre nuestras funciones | |
this.notifyToFirstTwoCountries.apply(this, filteredCountries); | |
return filteredCountries; | |
}, | |
notifyToFirstTwoCountries(firstCountry, secondCountry) {}, | |
applyFilters(region) { | |
// no más a reasignación condicional de parametros | |
region = region || 'LATAM'; | |
return this.filterByRegion(region, this.get('gtCountry'), this.get('usCountry')); | |
} | |
}); |
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.Object.extend({ | |
gtCountry: { code: 'GT', region: 'LATAM' }, | |
usCountry: { code: 'US', region: 'NA' }, | |
filterByRegion(region, ...countries) { // rest parameters | |
const filteredCountries = countries.filter((country) => country.region === region); | |
this.notifyToFirstTwoCountries(...filteredCountries); // spread operator | |
return filteredCountries; | |
}, | |
notifyToFirstTwoCountries(firstCountry, secondCountry) {}, | |
applyFilters(region = 'LATAM') { | |
return this.filterByRegion(region, this.get('gtCountry'), this.get('usCountry')); | |
} | |
}); |
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'; | |
const countries = [ 'Guatemala', 'United States' ]; | |
// object destructuring | |
const { | |
$, | |
RSVP, | |
inject: { service } | |
} = Ember; | |
// array destructuring | |
const [ guatemala, usa ] = countries; | |
// sustituye los patrones | |
const $ = Ember.$; | |
const service = Ember.inject.service; | |
const RSVP = Ember.RSVP; | |
const guatemala = countries[0]; | |
const usa = countries[1]; |
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.Object.extend({ | |
countries: [ | |
// a lot of countries defined here | |
], | |
filterByRegion: function(region) { | |
const countries = this.get('countries').findBy('region', region); | |
return { | |
region: region, | |
countries: countries | |
}; | |
} | |
}); |
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.Object.extend({ | |
countries: [ | |
// a lot of countries defined here | |
], | |
filterByRegion(region) { | |
const countries = this.get('countries').findBy('region', region); | |
return { | |
region, | |
countries | |
}; | |
} | |
}); |
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.Object.extend({ | |
getRegionDescription(region) {}, | |
fetchCountries(region) { | |
const self = this; | |
return fetch('/api/countries/' + region) | |
.then(function parseRegionValues(countries) { | |
const countriesByRegion = {}; | |
countriesByRegion[region] = { | |
label: self.getRegionDescription(region), | |
countries: countries | |
}; | |
return countriesByRegion; | |
}); | |
} | |
}); |
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.Object.extend({ | |
getRegionDescription(region) {}, | |
fetchCountries(region) { | |
return fetch(`/api/countries/${region}`) | |
.then((countries) => { | |
return { | |
[region]: { | |
label: this.getRegionDescription(region), | |
countries | |
} | |
}; | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment