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

compose-remote-form

v3.0.4

Published

A utility library for simplifying ajax form submission and managing response handlers.

Downloads

1

Readme

Compose Remote Form

Wraps good old forms and transforms them into AJAX forms.

Installation

npm install compose-remote-form

Usage

Setup

var RemoteForm = require('compose-remote-form')

Add your own ajax event callbacks

More details about each fired "event" below.


// Register single callbacks:
RemoteForm.on('#formid', 'error', function(form, body, status, xhr) {
  console.log('success!', body)
})

// Register multiple callbacks for a single form:
var formEl = document.querySelector('#formid')
RemoteForm.on(formEl, {
  beforeSend: function(form, req){
    // req is the current reuqest object
    console.log('submitting the form...')
  },
  success: function(form, eventType, xhr){
    console.log('success!', xhr.responseText)
  },
  error: function(form, eventType, xhr){
    console.log('error :(', xhr.errors)
  },
  complete: function(form, eventType xhr){
    // All form events have been triggered
    // Use this to do any generic cleanup
  },
})


// Listen to form events on all forms
RemoteForm.on(document, {
  success: function(form, body, status, xhr){
    console.log('a form was submited sucessfully!')
  },
})

Events

Without the need to bind any extra events, you can specify beforeSend, success and error functions in your extension of RemoteForm (shown in the example code above.)

ajax:beforeSend(form, req)

Fired before sending the AJAX request. There's no stopping it, but it's useful to notify the user that something is happening, disable buttons/inputs, etc.

It's fired with the superagent request (req) object.

ajax:success(form, body, status, xhr)

Upon success, this is fired with the returned body, response status and the ajax request xhr object.

ajax:error(form, xhr, status, error)

When an error occurs, this event is fired with the original ajax request xhr object and the error that the ajax library suffered.

It'll fire in the event of a request not getting through (due to CORS, server down, etc.), a server error (5xx) or a client error (4xx).

ajax:complete(form, xhr, status)

The complete event is fired at the end of the ajax submission lifecycle, regardless of success or failure. You might use this event to perform some cleanup action no matter the end result of a form submission.

Confirm dialog

If you add a data-confirm='Are you sure?' to your form's submission button to trigger a confirm dialog. This will use the browser's default confirm dialog but you can customize it like this if you wish.

var RemoteForm = require('compose-remote-form')

RemoteForm.confirm = function(options) {
  // your code.
}

The options passed to your confirm function will look like this:

options = {
  title: 'Are you sure?', // Based on the button's data-confirm attribute.
  submit: '#form-id',     // form's id, allowing you to trigger a javascript submit.
  message: '',            // OPTIONAL: button's data-message attribute, used to allow title/message style dialogs.
  destructive: true,      // OPTIONAL: Matches data-destructive attribute (used to set styling on warning style confirm dialogs
  follow: 'http://...',   // OPTIONAL: A data-follow attribute can be set to take a user to a url.
}