ember-co

2.0.0 • Public • Published

ember-co

Build Status npm Version Ember Observer Score

A tj/co implementation for Ember.

Installation

ember install ember-co

Usage

To create a Promise

import co from 'ember-co';
...
let companyNamePromise = co(function*() {
  let user = yield this.store.findRecord('user', 'Johnny');
 
  // assuming the User model has an asynchronous `belongsTo` relationship with Company model
  let company = yield user.get('company');
 
  // name can be both a synchronous and an asynchronous property
  return company.get('name');
}, 'promise label'); // label is optional
 
console.assert(companyNamePromise instanceof Ember.RSVP.Promise, 'co returns an Ember promise');
 
companyNamePromise.then(console.log)

To create a function that returns a Promise

import { wrap } from 'ember-co';
 
let UserRoute = Ember.Route.extend({
  model: wrap(function*({ id }) {
    let user = yield this.store.findRecord('user', id);
    this.set('user', user);
    let friends = user.get('friends');
    return { user, friends };
  }, 'model promise'), // label is optional
 
  actions: {
    updateCompanyName: wrap(function*(newName) {
      this.get('user');
      let company = yield user.get('company');
      company.set('name', newName);
    }, 'update company name action'), // label is optional
  }
});

Package Sidebar

Install

npm i ember-co

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

9.68 kB

Total Files

8

Last publish

Collaborators

  • akatov