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-cli-event-handlers

v0.0.5

Published

Simple extenstion for Components to automatically manage external event handlers.

Downloads

18

Readme

ember-cli-event-handlers

Build Status Ember Observer Score

This tiny addon introduces a syntactic sugar for handling external (jQuery) events in components.

You can create an event handler using the provided handle or handleManual descriptors similar to the computed property descriptors; the handler will be attached to the selected element on didInsertElement and will be detached on willDestroyElement. As long as Ember destroys an element after destroying a component, your handler will stop reacting once the component becomes unavailable. It's done by monitoring isDestroying/isDestroyed properties of the component.

Configuration

Add the mixin to the components in which it will be used. Also, there's an option to apply the mixin to every component in your application; to do this, use the following configuration option:

// in /config/environment.js
    "ember-cli-event-handlers": {
      autoApply: true
    }

Usage

Definition

Basically, this addon defines a new property descriptor handle that can be used as follows:

propertyName: handle(event, rootElement, selector, callback)

where:

event is one of the event names supported by jQuery, such as 'click', 'mouseover', etc.

rootElement: 'component' (used by default if this argument is omitted), 'window', or 'body'.

selector: a string defining the element to which the handler is attached (within the specified root element).

callback: a function that will handle the event; receives the triggered event as the argument.

You can omit rootElement (then the handler will be bound to the component's element) and/or selector.

Examples

import { handle, EventHandlersMixin } from 'ember-cli-event-handlers';

export default Ember.Component.extend(EventHandlersMixin, {
  
  // This will be attached to window object
  windowProp: handle('resize', 'window', function(event) {
    this.doSomeCoolStuffOnResize(event);
  }),
  
  // By supplying an optional selector you can attach handler
  // to the element inside of component template
  internalProp: handle('scroll', '.chat-container', function(event) {
    this.scrolled(event);
  })
});

Or, if you have enabled the autoApply mode:

import { handle } from 'ember-cli-event-handlers';

export default Ember.Component.extend({
  
  // Handler definitions will be the same
});

Additional methods

Let's assume you've attached the handler to the handlerProp property of your component. Then you can use the following methods:

this.handlerProp.off() to temporarily deactivate the specified event handler, and

this.handlerProp.on() to activate this handler again.

Also, when defining your handler, you can use handleManual descriptor instead of handle. The handlers defined this way will be active only after being enabled with .on(). Use handling property of handler to find out if it's on or off right now. Example:

import { handle, EventHandlersMixin } from 'ember-cli-event-handlers';

export default Ember.Component.extend(EventHandlersMixin, {
  
  // This handler will only start working once it's activated with on()
  scrollHandler: handleManual('scroll', '.chat-container', function(event) {
    this.scrolled(event);
  }),

  // ...

  actions: {
    // The scroll handler activates when this action is triggered
    enableScrolling() {
      this.scrollHandler.on();
    },

    // This action makes the scroll handler inactive
    disableScrolling() {
      this.scrollHandler.off();
    }
  }
});