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

stimulus-remote

v2.2.1

Published

Rails UJS bindings for Stimulus

Downloads

329

Readme

Stimulus Remote

Stimulus controller to provide content handling for HTML sent over the wire whilst using Rails UJS

Installation

$ yarn add stimulus-remote

Usage

Register the controller with Stimulus:

// application.js
import { Application } from 'stimulus';
import { RemoteController } from 'stimulus-remote'

const application = Application.start()
application.register('remote', RemoteController)

Initialize the controller on a container element, and add an action to render the response:

<%= form_with model: @model, data: { controller: 'remote', action: 'ajax:error->remote#replace' } do |f| %>
  <%= f.text_field :field_name %>
  <%= f.submit %>
<% end %>

In the Rails controller, you will need to modify your response:

def create
  @model = Model.new model_params
  if @model.save
    redirect_to models_path, notice: 'Success!'
  else
    render partial: 'form', status: :unprocessable_entity
  end
end

You can see that the form partial has been separated so it is the only thing returned in the response. The response also sets an Unprocessable Entity HTTP Status Code (422). This is important so that Rails UJS emits a 'ajax:error' event.

Options

data-remote-load-scripts (default: false)

Setting this attribute on the controller element will eval() scripts in the response if set. This is intentionally off by default.

Example
<div data-controller="remote" data-remote-load-scripts>
  <%= link_to 'Click me', foo_path, data: { action: 'ajax:success->remote#replace' } %>
</div>

data-remote-response-target (default: this.element)

Use this attribute to change the element that will have its contents changed. This can be any CSS selector and does not have to be within the scope of the controller.

If you have more than one event that you wish to work with, and each event should work on a different part of the document (for example, ajax:error should replace the form, but ajax:success should append the response to another part of the DOM), then you can use a JSON hash. In this instance, missing event targets will resolve to this.element which is the element the controller is bound to.

Basic Example
<div id="response-target"></div>
<div data-controller="remote" data-remote-response-target="#response-target">
  <%= link_to 'Click me', foo_path, data: { action: 'ajax:success->remote#replace' } %>
</div>
Advanced Example
<div id="response-target"></div>
<div data-controller="remote" data-remote-response-target="{'ajax:success': '#response-target'}">
  <%= link_to 'Click me', foo_path, data: { action: 'ajax:error->remote#replace ajax:success->remote#append' } %>
</div>

data-remote-debounce-time (default: 300)

Change the debounce time for the debouncedSubmit method in ms.

Methods

submit

Submits the form related to the input element using Rails.fire(). This is for use on change events on input elements.

Example
<%= form_with model: @model, data: { controller: 'remote', action: 'ajax:error->remote#replace' } do |f| %>
  <%= f.text_field :field_name, data: { action: 'change->remote#submit' } %>
  <%= f.submit %>
<% end %>

debouncedSubmit

The same as submit but debounced. This allows you to submit a form when the user has finished typing in a field.

<%= form_with model: @model, data: { controller: 'remote', action: 'ajax:error->remote#replace' } do |f| %>
  <%= f.text_field :field_name, data: { action: 'input->remote#debouncedSubmit' } %>
  <%= f.submit %>
<% end %>

replace

The replace method is used to replace the response target.

Example
= form_with model: @model, data: { controller: 'remote', action: 'ajax:error->remote#error' } do |f|
  = f.text_field :field_name
  = f.submit

replaceInner

The replaceInner method is used to replace the contents of the response target (innerHTML).

Example
<div data-controller="remote">
  <%= link_to 'Click me to replace me', foo_path, data: { action: 'ajax:success->remote#replaceInner' } %>
</div>

append

The append method is used to append the content of the response to the response target.

Example
<div data-controller="remote">
  <%= link_to 'Click me to append my response', foo_path, data: { action: 'ajax:success->remote#append' } %>
  <div>Some content</div>
  <!-- will append response here -->
</div>

prepend

The replace method is used to prepend the content of the response to the response target.

Example
<div data-controller="remote">
  <!-- will append response here -->
  <div>Some content</div>
  <%= link_to 'Click me to prepend my response', foo_path, data: { action: 'ajax:success->remote#append' } %>
</div>

remove

The remove method is used to remove the response target. By default, the response target is the element on which the controller is instantiated.

Example
<div data-controller="remote">
  <div>Some content</div>
  <%= link_to 'Click me to remove me', foo_path, data: { action: 'ajax:success->remote#remove' } %>
</div>

Contributing

Fork the project.

Install dependencies

$ yarn install

Start the test watcher

$ yarn test:watch

Running one-off test runs can be done with:

$ yarn test

Write some tests, and add your feature. Send a PR.