Coder Social home page Coder Social logo

jmurphyau / ember-api-actions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mike-north/ember-api-actions

0.0 1.0 0.0 2.24 MB

Trigger API actions in ember.js apps

Home Page: http://mike.works/ember-api-actions/

License: MIT License

JavaScript 77.48% HTML 11.72% CSS 0.42% Handlebars 10.37%

ember-api-actions's Introduction

Ember-api-actions

Build Status Dependency Status devDependency Status Ember Observer Score npm version

Trigger "remote" actions on ember-data resources that don't fit into typical CRUD RESTful API design.

For example, if you have restful API endpoints like

GET     /fruits
POST    /fruits
GET     /fruits/123
PUT     /fruits/123
DELETE  /fruits/123

What happens if you want to consume API endpoints like these:

PUT    /fruits/123/ripen
GET    /fruits/citrus

Example Rails API routes:

config/routes.rb

Rails.application.routes.draw do

  resources :fruits do
    member do
      put 'ripen' to: 'fruits#ripen'
    end
    collection do
      get 'citrus' to: 'fruits#citrus_index'
    end
  end

end

This is not immediately intuitive with ember-data, and these kinds of API endpoints can be found in widely-used RESTful APIs. This library aims to make it easy.

Use

# ember-cli >= 0.2.3
ember install ember-api-actions

# ember-cli < 0.2.3
ember install:addon ember-api-actions

You can then add these "actions" (not to be confused with client-side ember.js actions) to your ember-data model

app/models/fruit.js

import DS from 'ember-data';
import { memberAction, collectionAction } from 'ember-api-actions';

const { attr } = DS;

export default DS.Model.extend({
  name:     attr('string'),
  // /fruits/123/ripen
  ripen:        memberAction(    { path: 'ripen' }),
  // /fruits/citrus
  getAllCitrus: collectionAction({ path: 'citrus' })
});

you can then call these functions, and they will initiate API requests and return you the promise

// Pass data in, it will be sent in the POST or PUT request payload
myRecord.ripen({someData: 'abc'}).then(response => {
  // do something when the API returns a response
});

Customization

ember-api-actions generates URLs and ajax configuration via ember-data adapters. It will identify the appropriate adapter, and call the buildURL and ajaxOptions methods to send a JSON request similar to way conventional ember-data usage works.

Customizing your adapter should customize requests sent out via this library, along with any other ember-data requests.

ember-api-actions uses the following methods on DS.Adapter

  • buildURL - for generating an action's URL
  • ajax (private) - to actually make the API request and return a promise

Installation

  • git clone this repository
  • npm install
  • bower install

Running

Running Tests

  • ember test
  • ember test --server

Building

  • ember build

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

ember-api-actions's People

Contributors

ember-tomster avatar mike-north avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.