Upgrade to Pro — share decks privately, control downloads, hide ads and more …

15 Things You Shouldn't Do In Ember Anymore

15 Things You Shouldn't Do In Ember Anymore

Kerrick Long

August 03, 2015
Tweet

More Decks by Kerrick Long

Other Decks in Programming

Transcript

  1. Kerrick Long Things I make and do Where to find

    me online twitter.com/KerrickLong github.com/Kerrick Lead Front-end Developer at Second Street KerrickLong.com www. meetup.com/ STLEmber
  2. import Ember from 'ember';
 
 export default Ember.Controller.extend({
 needs: ['pages'],


    title: 'controllers.pages.model.title'
 });
 Using needs in Controllers
  3. import Ember from 'ember';
 
 export default Em.Service.extend({
 minutes: 480,


    hours: Em.computed('minutes', function(k, v) {
 if (arguments.length > 1) {
 this.set('minutes', v * 60);
 }
 
 return this.get('minutes') / 60;
 })
 }); Computed getter / setter
  4. Computed getter / setter import Ember from 'ember';
 
 export

    default Em.Service.extend({
 minutes: 480,
 hours: Em.computed('minutes', {
 get() {
 return this.get('minutes') / 60;
 },
 set(k, v) {
 return this.set('minutes', v * 60);
 }
 })
 });
  5. Using this.resource Router.map(function() {
 this.resource('pages', function() {
 this.resource('comments');
 });
 });

    // app/routes/comments/index.js
 export default Ember.Route.extend({
 model() {
 return this.store.find('comments');
 }
 });
  6. Using this.resource Router.map(function() {
 this.route('pages', function() {
 this.route('comments');
 });
 });


    // app/routes/pages/comments/index.js
 export default Ember.Route.extend({
 model() {
 return this.store.find('comments');
 }
 });
  7. Using arrayComputed import Ember from 'ember';
 
 export default Ember.Controller.extend({


    uniqueChildren: Ember.arrayComputed('[email protected].[]', {
 addedItem: function(accumulatedValue, model) {
 accumulatedValue.addObjects(model.get('children'));
 return accumulatedValue.uniq();
 },
 removedItem: function(accumulatedValue, model) {
 accumulatedValue.removeObjects(model.get('children'));
 return accumulatedValue.uniq();
 }
 })
 });
  8. Using arrayComputed import Ember from 'ember';
 
 export default Ember.Controller.extend({


    uniqueChildren: Ember.computed('[email protected].[]', function() {
 return _.flatten(this.get('model').map(x => x.get('children'))).uniq();
 })
 });
  9. Using reduceComputed import Ember from 'ember';
 
 export default Ember.Controller.extend({


    total: Ember.reduceComputed('[email protected]', {
 initialValue: 0,
 addedItem(accumulatedValue, item) {
 return accumulatedValue + item.get('value');
 },
 removedItem(accumulatedValue, item) {
 return accumulatedValue - item.get('value');
 }
 })
 });
  10. Using reduceComputed import Ember from 'ember';
 
 export default Ember.Controller.extend({


    total: Ember.computed('[email protected]', function() {
 const addValues = (p, x) => p + x.get('value');
 return this.get('model').reduce(addValues, 0);
 })
 });
  11. Using ArrayController import Ember from 'ember';
 
 export default Ember.ArrayController.extend({


    sortProperties: ['date', 'name']
 // You can use `arrangedContent`
 });