danmcclain / ember-deferred-content

MIT License
85 stars 5 forks source link

ember-deferred-content

Fancy pants handling of async content

Greenkeeper badge Build Status npm version

ember install ember-deferred-content

Usage

  {{! This assumes that post has an async relationship called comments}}
  {{#deferred-content post.comments as |d|}}
    {{#d.settled}}
      <h2>Comments</h2>
    {{/d.settled}}
    {{#d.pending}}
      <img src="https://github.com/danmcclain/ember-deferred-content/raw/master/spinner.gif">
    {{/d.pending}}
    {{#d.fulfilled as |comments|}}
      <ul>
        {{#each comments as |comment|}}
          <li>{{comment.author}} said: {{comment.body}}
        {{/each}}
      </ul>
    {{/d.fulfilled}}
    {{#d.rejected as |reason|}}
      Could not load comments: {{reason}}
    {{/d.rejected}}
  {{/deferred-content}}

  {{! or using ifs}}
  {{#deferred-content promise=post.comments as |d|}}
    {{#if d.isSettled}}
      <h2>Comments</h2>
    {{/if}}
    {{#if d.isPending}}
      <img src="https://github.com/danmcclain/ember-deferred-content/raw/master/spinner.gif">
    {{/if}}
    {{#if d.isFulfilled}}
      <ul>
        {{#each d.content as |comment|}}
          <li>{{comment.author}} said: {{comment.body}}
        {{/each}}
      </ul>
    {{/if}}
    {{#if d.isRejected}}
      Could not load comments: {{d.content}}
    {{/if}}
  {{/deferred-content}}

ember-deferred-content takes the promise you need to resolve to show your content, and yields 4 subcomponents that you can use to show content during the different states of your promise

It also sets a series of flags:

Compatibility

This addon will work on Ember versions 2.3.x and up only, due to use of contextual components and the (hash helper.

Developing

Running Tests