Skip to content

Instantly share code, notes, and snippets.

@jonathansamines
Created March 9, 2016 06:46
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 jonathansamines/214ac02a5f5da0cc2110 to your computer and use it in GitHub Desktop.
Save jonathansamines/214ac02a5f5da0cc2110 to your computer and use it in GitHub Desktop.
ES6 sample code
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'));
}
});
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'));
}
});
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];
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
};
}
});
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
};
}
});
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;
});
}
});
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