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

ajax-event

v3.0.2

Published

Makes it easier to build GUIs that rely on AJAX resources

Downloads

19

Readme

AjaxEvent

A utility to make tracking the status of your AJAX requests easier. Use it with MVC frameworks to reduce the complexity of your View logic.

Overview

10-Second Example

Have you ever wanted to simplify your view logic so it's more self-explanatory? Imagine if your views looked like this (using Angular for example):

<p *ngIf="fetchAccountEvent.isExecuting()">
    Your account is loading...
</p>

<p *ngIf="fetchAccountEvent.hasError()">
    {{ fetchAccountEvent.message }}
</p>

<div *ngIf="fetchAccountEvent.isSuccessful()">
    Your account details: {{ fetchAccountEvent.data }}
</div>

Here's the code that could make that happen:

var fetchAccountEvent = new AjaxEvent();

// Set the internal state to 'executing' while the request is pending
fetchAccountEvent.resetToExecuting();

$.ajax({
    url: 'yourapi.com/account',
    method: 'GET',
    success(response) {
        // Everything went ok, 
        // store a success message and any associated data (ie: the payload)
        fetchAccount.resolve(response.account_object, 'The account was fetched successfully');
    },
    error(err) {
        // Something went wrong, 
        // store an error message and associated error data
        fetchAccountEvent.reject(err, 'There was an error! Error code: ' + err.error_code);
    }
});	

API Summary

An AjaxEvent is a chainable object that has the following basic properties:

  • status: The current state of your request
  • message: A string to describe the current state of the request (often used for errors)
  • data: A simple container that can be used to store the result of the request

These properties can be set as appropriate to make use of the following quick functions:

  • isSuccessful(): Check if the request has completed successfully
  • isExecuting(): Check if the request is in the middle of being executed
  • isNotExecuting(): Check if the request is not in the middle of being executed
  • isReady(): Check if the request has not been executed yet
  • hasError(): Check if the request has encountered an error
  • hasData(): Check if the request has completed and has stored data in the AjaxEvent
  • hasMessage(): Check if the request has set a message in the AjaxEvent

Usage

Installation

With Yarn do:

yarn add ajax-event

Then in your JS, make the package available:

import AjaxEvent from 'ajax-event';

Reacting To Network Requests

Level 1: Ultra-Basic

The simplest example just uses resolve() and reject() to set the AjaxEvent's internal status:

var someEvent = new AjaxEvent();

// Perform an AJAX request (example with Promises)
getSomeData
    .then(() => someEvent.resolve()) // Everything went ok
    .catch(() => someEvent.reject()) // Something went wrong

This will allow you to use AjaxEvent in your view to show how things are going:

<p *ngIf="someEvent.hasError()">
    Oops, something went wrong! 
    <button>Try Again</button>
</p>

<div *ngIf="someEvent.isSuccessful()">
    Everything is all good!
</div>

Level 2: Standard Usage

The AjaxEvent is also the perfect place to store your response messages and payload data. You can also re-use the instances multiple times.

// Create a re-usable AjaxEvent instance
var someEvent = new AjaxEvent();

function refreshLatestData() {
    // Clear any existing data and messages, and set the internal state to 'executing'
    someEvent.resetToExecuting();
    
    // Perform an AJAX request (example with Promises)
    getSomeData.then(function(response) {
        // Everything went ok, store any associated data (ie: the payload)
        // and a success message
        someEvent.resolve(response.data, 'Request successful!');
    }).catch(function(response) {
        // Something went wrong, store associated error data
        // and an error message
        someEvent.reject(response.error, 'Something went wrong!');
    });
}

// Do a network request every 5 seconds
setInterval(refreshLatestData, 5000);

You can react to when the requests are pending (ie: sent, but not finished):

<p *ngIf="someEvent.isExecuting()">
    <img src="loading-spinner.gif">
    Loading...
</p>

<p *ngIf="someEvent.hasError()">
    Oops, something went wrong:
    {{ someEvent.message }}
</p>

<div *ngIf="someEvent.isSuccessful()">
    The latest data is:
    {{ someEvent.data }}
</div>

Running Tests

Clone this project into your local computer and run the following in its root directory (where the package.json is):

yarn install
yarn test

License

Released under the MIT license.

What's New

v3.0.1

Added optional generic type new AjaxEvent<DataT>() for TS IDE assistance when working with the data field, it will now have its shape known if DataT is provided during construction.

v3.0.0

The data will no longer be lost when calling resetToExecuting(), it will persist until some other command is called.