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-context

v0.1.0

Published

React-style context for Ember.

Downloads

16

Readme

ember-context

React-style context for Ember.

What is it?

If you're tired of passing properties many levels down the component tree, ember-context may be the answer you've been looking for.

Motivation

React offers a context approach which allows sharing data from ancestor components to descendents without having to pass it directly down the component tree. This addon is an attempt to emulate that behaviour in Ember.

Disclaimer

Context is usually not the best tool for the job, as it has some drawbacks, both stylistically and in terms of efficiency. If you still feel like context is the best solution for your design, then go ahead and use it :wink:

Usage

Usage is very similar to the React usage.

Create a context

Import and invoke ember-context's createContext function, passing a default value for the context:

/* contexts/my-context.js */

import createContext from 'ember-context';

export default createContext('some default value');

This will return a tuple of Provider and Consumer components, and a ConsumerMixin mixin to be used with your own components.

Render a provider component

Use the Provider component returned by the createContext call. You can do this by adding a provider component to your application and re-exporting your context's Provider component:

/* components/my-context/provider */

import myContext from '../../contexts/my-context';

export default myContext.Provider;

then rendering that component in your application and optionally passing a value (the default value passed to createContext will be used otherwise):

{{!-- templates/application.hbs --}}

{{#my-context/provider value="my context's value"}}
  ...
{{/my-context/provider}}

Consume the context in a child component

There are two possible ways of achieving this - you can either use your context's Consumer component, or its ConsumerMixin in one of your own components.

Using the Consumer component

Similar to rendering the Provider component, you can add a consumer component to your application and re-export your context's Consumer component:

/* components/my-context/consumer */

import myContext from '../../contexts/my-context';

export default myContext.Consumer;

then render that component in your application. The consumer component will yield your context's value:

{{!-- templates/application.hbs --}}

{{#my-context/provider value="my context's value"}}
  ...
    {{#my-context/consumer as |context|}}
      <span>{{context}}</span>
    {{/my-context/consumer}}
  ...
{{/my-context/provider}}

Using the ConsumerMixin in your own components

If you'd prefer direct access to your context inside your own components, you can use your context's ConsumerMixin:

/* components/my-component.js */

import Component from '@ember/component';
import { computed } from '@ember/object';
import myContext from '../contexts/my-context';

export default Component.extend(myContext.ConsumerMixin, {
  // component has a `context` property which will be
  // automatically populated, which we can use internally.
  myValue: computed('context', function() {
    return `Context value is '${this.get('context')}'`;
  }),
});

You then render your component inside a provider to give it access to the context:

{{!-- templates/application.hbs --}}

{{#my-context/provider value="my context's value"}}
  ...
  {{my-component}}
  ...
{{/my-context/provider}}

Updating the context's value

You can pass any value into your context's Provider component, including variables. Any updates to the context's value will propagate automatically:

/* controllers/application.js */

import Controller from '@ember/controller';

export default Controller.extend({
  timesClicked: 0,

  actions: {
    incrementCounter() {
      this.incrementProperty('timesClicked');
    },
  },
});
{{!-- templates/application.hbs --}}

{{#my-context/provider value=timesClicked}}
  <button {{action "incrementCounter"}}>
    {{#my-context/consumer as |context|}}
      <span>Clicked {{context}} times</span>
    {{/my-context/consumer}}
  </button>
{{/my-context/provider}}

Installation

  • ember install ember-context

## Contributing

Installation

  • git clone <repository-url>
  • cd my-addon
  • npm install

Linting

  • npm run lint:hbs
  • npm run lint:js
  • npm run lint:js -- --fix

Running tests

  • ember test – Runs the test suite on the current Ember version
  • ember test --server – Runs the test suite in "watch mode"
  • ember try:each – Runs the test suite against multiple Ember versions

Running the dummy application

For more information on using ember-cli, visit https://ember-cli.com/.

## License

This project is licensed under the MIT License.