npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-loading

v2.0.0

Published

Primitives for handling loading state in Ember apps

Downloads

4,371

Readme

ember-loading

CI Ember Observer Score npm version

A simple Ember.js addon to capture and expose the loading state of your app, so you can show that to the user with e.g. a loading indicator.

But Ember has loading substates so why do I even need it?

This is true, and works basically like this: when the user transitions from /foo to /bar, the DOM previously rendered through foo.hbs will be removed, the loading.hbs template renders while loading of the bar route is in progress, after which bar.hbs is rendered. That's fine, if it is what you need.

But another UX approach would be to keep foo.hbs rendered and add any kind of loading indicator on top of it (e.g. an overlay over the existing content) or somewhere else on the page while loading, and replace the rendered foo.hbs with bar.hbs only when loading has finished.

What else?

This addon helps you also to show the loading state for async processes besides transitioning between routes, e.g. when you load or save data in a controller/component.

How does it look like?

It is completely agnostic of any looks. It merely provides some basic infrastructure to capture and expose the loading state. To actually render your loading indicator, you can use your own custom styled component or any of the existing loading indicator addons.

Compatibility

  • Ember.js v3.20 or above
  • Ember CLI v3.20 or above
  • Node.js v12 or above

Installation

ember install ember-loading

Usage

The addon's central piece is the loading service, which captures and exposes the app's loading state.

Exposing the loading state

Using the service

The loading service exposes the following properties, which you can use to render your loading indicator in any way:

| Property | Description | |------------------------|-----------------------------------------------------------------------| | isLoading: Boolean | Will be true if any captured async processes (see below) are pending. | | showLoading: Boolean | Will be true based on isLoading, but will take optional preDelay and postDelay into account, to optimize for the visual appearance. See Configuration below. |

Using while-loading

Rather than explicitly injecting the loading service and using an {{#if}} block in your template, you can more conveniently use the while-loading component to wrap any content that should be shown only while loading:

{{#while-loading}}
  <LoadingIndicator/>
{{/while-loading}}

Capturing the loading state

Route transitions

The service will automatically recognize route transitions (i.e. async model hooks) and set the service's loading state accordingly.

Custom actions

Whenever you kick off async processes that you want to show to the user, and which are not part of any of a route's model hooks, you can use the service's run() function, which will call your own async function, and set the services's loading properties accordingly.

import Controller from '@ember/controller';
import { inject as service } from '@ember/service';

export default Controller.extend({
  loading: service(),

  actions: {
    save(model) {
      return this.loading.run(() => model.save());
    }
  }
})

The run() method has the following signature, similar to many of Ember's runloop functions:

| Arguments / Return value | Description | |----------------------------|----------------------------------------------------------------------| | target: any | optional target of method to call | | method: Function\|string | Async method to invoke. May be a function or a string. If you pass a string then it will be looked up on the passed target. | | ...args: any[] | Any additional arguments you wish to pass to the function. | | returns | Promise that resolves with the return value of the invoked function. |

Decorator

If you use native classes and decorators, you can use the supplied @loading decorator to wrap any native method.

The decorator uses stage 1 syntax, meant to be used with TypeScript and/or ember-cli-babel V7.7.0+

import Controller from '@ember/controller';
import { action } from '@ember-decorators/object';
import { loading } from 'ember-loading';

export default class Foo extends Controller {

  @action
  @loading
  save(model) {
    return model.save();
  }
}

Configuration

The addon supports the following configuration options in your config/environment.js, under the ember-loading key:

| option | Default | Description | |-------------------------------|---------|------------------------------------------------------------| | preDelay: number | 0 | Amount of milliseconds to delay the showLoading property (see above) going from false to true. This allows you to suppress the loading indicator appearing for very short loading times. | | postDelay: number | 0 | Amount of milliseconds to delay the showLoading property (see above) going from true to false. This can help you with aggregating multiple async processes happening in succession, to prevent flickering of the loading indicator. | | watchTransitions: Boolean | true | If false, async transitions do not affect the isLoading property. |

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.