These are chat archives for mydigitalself/thoughts2

12th
May 2015
Mike Bartlett
@mydigitalself
May 12 2015 15:04
profile pic 1.jpg
Mike Bartlett
@mydigitalself
May 12 2015 15:15
// ES6: Reusing the same loop variable name is OK
var counter = 0;
for(let i = 0; i < 3; i += 1) {
  for(let i = 0; i < 3; i += 1) {
    counter += 1;
  }
}

console.log(counter); // Prints "9"
Mike Bartlett
@mydigitalself
May 12 2015 15:21
img1.ready().then(function() {
  // loaded
}, function() {
  // failed
});

// and…
Promise.all([img1.ready(), img2.ready()]).then(function() {
  // all loaded
}, function() {
  // one or more failed
});
```js
img1.ready().then(function() {
  // loaded
}, function() {
  // failed
});

// and…
Promise.all([img1.ready(), img2.ready()]).then(function() {
  // all loaded
}, function() {
  // one or more failed
});
img1.ready().then(function() {
  // loaded
}, function() {
  // failed
});

// and…
Promise.all([img1.ready(), img2.ready()]).then(function() {
  // all loaded
}, function() {
  // one or more failed
});
shared dynamic AppInterface {
    shared formal void listen(Integer port, Anything() callback);
}
Mike Bartlett
@mydigitalself
May 12 2015 16:56
import { Events } from 'backbone';
import _ from 'underscore';
import { hash } from '../lib/rsvp';
import Region from './region';
import globalCh from '../utils/globalCh';

export default class Application {
  constructor(options){
    this._initializers = {};
    this.regions = {};
    _.extend(this, Events);
  }

  start(options = {}){
    this.trigger('before:start', options);

    let initializers = _.reduce(this._initializers, (memo, func, key) => {
      memo[key] = func.call(this, options);
      return memo;
    }, {});

    hash(initializers).then((results) => {
      _.extend(options, results);
      this.trigger('start', options)
    }).catch((e) => {
      console.error(e, e.stack);
    });
  }

  /**
   * Initializers are asynchronous, and need to return a Promise (or 'thenable'
   * object). The initializers are all run after 'before:start', and
   * before 'start' (great, right?). The results of the Promises hash will be
   * mixed into the options object passed into `Application#start`. Be aware
   * that these will not be available in functions bound to 'before:start'.
   *
   * @param {string} key      The key under which the result will become
   *                          available after the promise has resolved.
   *
   * @param {function} func   The initializer function to call.
   *                          Must return Promise.
   */
  addInitializer(key, func){
    this._initializers[key] = func;
  }

  addRegions(regions){
    if(typeof regions !== 'object'){
      throw new Error(`regions argument should be an object, is ${typeof regions}`);
    }
    _(regions).each(this.addRegion, this);
  }

  addRegion(regionConfig, key){
    if(_.isObject(regionConfig)){
      return this.addRegionByHash(regionConfig, key);
    }
    this.regions[key] = this[key] = new Region({ el: regionConfig });
    globalCh.reply(`region:${key}`, () => this[key]);
  }

  addRegionByHash(regionConfig = { regionClass: Region }, regionKey){
    let { regionClass, el } = regionConfig;
    this.regions[regionKey] = this[regionKey] = new regionClass({ el });
    globalCh.reply(`region:${regionKey}`, () => this[regionKey]);
  }

}