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-jsonapi-resources-form

v1.0.0-beta

Published

Ember Addon: JSON API Resource form components.

Downloads

1

Readme

Ember-jsonapi-resources-form

Ember Addon that provides components for use with forms to edit a resource that follows JSON API 1.0 specification.

Example Use

For a simple example see the address-form component usage in the dummy app.

The resource-form is an abstract component for CRUD operations on a Resource instance (model).

To trigger actions include the form controls component in the template for your component that extends resource-form, and set the target…

Example template for a form component that extends the resource-form:

{{form-controls target=this isEditing=isEditing isNew=isNew isAdmin=isAdmin}}
{{text-input value=resource.name
  name="name" action="performUpdate" target=this
  requireInput=isEditing disabled=notEditing}}
{{number-input value=resource.total min="0" step="1"
  name="total" action="applyChange" target=this
  requireInput=isEditing disabled=notEditing}}

Actions that interact with the various presentation modes of the form, include: toPreview (new resource), toDetail / toEdit (persisted resource), cancel, save, and destroy.

A few examples using a (concrete) component which extends resource-form

For an "editing" route or 'editing' state of the form:

{{catalog-form model=model isNew=model.isNew isEditing=true
  on-update=(route-action 'update')
  on-toDetail=(route-action 'toDetail')
}}

For a "Detail" route or "Preview" state of the form:

{{catalog-form model=model isNew=model.isNew isEditing=false
  on-edit=(route-action 'toEdit')
  on-destroy=(route-action 'destroy')
}}

For a "New" route / state of the form:

{{catalog-form model=model isNew=model.isNew isEditing=false
  on-save=(route-action 'save')
  on-cancel=(route-action 'cancel')
}}

A example form that adds behavior to edit relationships…

Form component

import Ember from 'ember';
import ResourceFormComponent from 'core-form/components/resource-form';

const { get, set, inject, computed } = Ember;

export default ResourceFormComponent.extend({
  products: inject.service(),
  resourceName: 'catalog',

  /**
    Handles selected relation for new Resource

    @method relationSelected
    @param {ProductResource} relation
  */
  relationSelected(relation) {
    let model = get(this, 'model');
    model.addRelationship(get(relation, 'type'), get(relation, 'id'));
  },

  /**
    @method unsetRelation
    @param {ProductResource} relation
  */
  unsetRelation(relation) {
    let model = get(this, 'model');
    model.removeRelationship(get(relation, 'type'), get(relation, 'id'));
  },

  /**
    @method addRelationship
    @param String resourceName
    @param String id
  */
  addRelationship(resourceName, id){
    let model = get(this, 'model');
    model.addRelationship(resourceName, id);
    get(this, get('resourceName')).patchRelationship(model, resourceName, id);
  },

  /**
    @method removeRelationship
    @param  {String} resourceName
    @param  {String} id
  */
  removeRelationship(resourceName, id){
    let model = get(this, 'model');
    model.removeRelationship(resourceName, id);
    get(this, get('resourceName')).destroyRelationship(model, resourceName, id);
  }
});

Form template

{{form-controls target=this isEditing=isEditing isNew=isNew inFlight=inFlight}}
<fieldset>
  <legend>Catalog Info</legend>
  {{#input-row label="Region" name="region"}}
    {{! text-input is bound to a value, use action to apply changes and persist}}
    {{text-input
      value=resource.region
      name="region"
      requireInput=isEditing
      disabled=notEditing
      action="performUpdate" target=this
    }}
  {{/input-row}}

  {{#input-row label="Total" name="total"}}
  {{! number-input doesn't bind to the value need, use action to set changes}}
  {{number-input min="0" step="15"
    value=resource.total
    name="total"
    requireInput=isEditing
    disabled=notEditing
    action="applyChange" target=this
  }}
  {{/input-row}}

  {{#input-row label="Product" name="product"}}
    {{#power-select
      placeholder="Choose a Product"
      disabled=notEditing
      selected=resource.product
      onchange=(action "selected")
      search=(action "search") as |product|}}
      {{product.name}}
    {{/power-select}}
  {{/input-row}}
</fieldset>
{{yield}}

Installation

In the consuming application…

ember install ember-jsonapi-resources-form

Development

Install dependencies…

  • git clone this repository
  • npm install
  • bower install

Running Tests

  • npm test (Runs ember try:testall to test your addon against multiple Ember versions)
  • ember test
  • ember test --server

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