ember-data-ensure

0.0.1 • Public • Published

Ember-data-ensure

A collection of utility functions to make gathering data from Ember Data easier, avoiding nested callbacks to resolve promises.

Define a scheme to fetch data. The scheme is a nested JS object with the names of relations as keys. In the scheme there is no distinction between attributes, and belongsTo and hasMany relationships. Nest them however you like. The scheme looks especially nice in CoffeeScript.

scheme = {
  'cities':{
    'name',
    'streets':{
      'name',
      'houses':{'number'}
      }
    }
  }

As JavaScript doesn't have notation for data types, fetching an attribute (or unresolved relationship) is indicated by a key with a value that's not a JS object. In CoffeeScript this can be achieved simply by leaving out the value.

The basic usage is ensure(object, scheme) which results in a Promise. Resolving it gives you the ability to use Ember's get directly without having to worry about resolving promises.

There are two variants that provide some additional functionality.

Most commonly, the scheme will describe all the data you want. ensureHash(object, scheme) returns a Promise which holds a hash with a structure as described in the scheme. This avoids repeated usage of Ember's get. It's already used under the hood, and it comes with an overhead. Note that you will still need to use get to access attributes that aren't named in the scheme.

Another common use-case is to collect data from various relationships. ensureCollect(object, scheme) returns a Promise which holds a list with all the values from the attributes. This avoids having to traverse what's essentially the same data structure multiple times.

Readme

Keywords

Package Sidebar

Install

npm i ember-data-ensure

Weekly Downloads

1

Version

0.0.1

License

MIT

Last publish

Collaborators

  • vincentgoossens