Giter Club home page Giter Club logo

ember-data-actions's Introduction

ember-data-actions

Introduces resource and collection actions to the Ember Data API, a la Rails. Actions are non-CRUD based endpoints in your RESTful API. Resource actions are actions performed on a single resource (i.e. "publish a book"), while collection actions are performed on the collection of resources (i.e. "a list of published books").

Setup

To use resource or collection actions, you must include the appropriate mixins in your models and adapters.

Your adapters should apply the ember-data-actions/mixins/adapter mixin:

// app/adapters/application.js
import DS from 'ember-data';
import AdapterActionsMixin from 'ember-data-actions/mixins/adapter';

export default DS.RESTAdapter.extend(AdapterActionsMixin);

Then, on any models with resource actions, use the resource mixin:

// app/models/book.js
import DS from 'ember-data';
import ResourceActionsMixin from 'ember-data-actions/mixins/resource';

export default DS.Model.extend(ResourceActionsMixin);

Finally, for any collection actions, use the collection mixin applied to the model class itself:

// app/models/book.js
import DS from 'ember-data';
import CollectionActionsMixin from 'ember-data-actions/mixins/collection';

var BookModel = DS.Model.extend();
BookModel.reopenClass(CollectionActionsMixin);

export default BookModel;

Invoking Actions

Resource Actions

// Assuming an API that responds to POSTs to /books/:id/publish, ...

var book = this.store.getById('1');
book.publish({ publishingHouse: 'Tomster, Inc.' }).then(function(result) {
  if (result.wasPublished) {
    alert('Yay!');
  }
});

//
// Results in:
//
// Request:
// POST /books/1/publish
// { publishingHouse: 'Tomster, Inc.' }
//
// Response:
// { wasPublished: 'Yay!' }
//

Invoking Collection Actions

store.action('book', 'createWithCover', { cover: cover })
.then(function(response) {
  // ...
}))

//
// Results in:
//
// Request:
// POST /books/create-with-cover
// { cover: cover }
//

Defining Actions

By default, triggered actions will result in an HTTP POST to the endpoint with the action's name (i.e. book.publish() -> /books/:id/publish, store.action('book', 'createWithOptions', options) -> /books/create-with-options), and returns a promise that resolves with the API response.

You can override this, either at the adapter layer to control the network communication (e.g. call a different endpoint), and/or at the model layer (e.g. create models from the responses).

If the default behavior works for you, then all you need to do is use the action macro to define resource actions on your models. Collection actions don't need to be previously defined.

import DS from 'ember-data';
import ResourceActionsMixin from 'ember-data-actions/mixins/resource';
import action from 'ember-data-actions/action';

var BookModel = DS.Model.extend(ResourceActionsMixin, {
  publish: action('publish')
});

Custom Resource Actions

If the default POST behavior isn't enough, you can customize the behavior of the action by defining an action handler on either the model or the adapter (or both):

// app/models/book.js
export default DS.Model.extend(ResourceActionsMixin, {
  publish: action('publish'),

  actions: {
    publish: function(adapterAction, params) {
      // params = the params object passed in when the action is invoked
      // Default implementation is to invoke adapterAction(params)
      //
      // Overriding this is useful if you want to do things like take a
      // response and insert a record into the store via
      // this.store.createRecord
    }
  }
});

// app/adapters/book.js or app/adapters/application.js
export default DS.RESTAdapter.extend(AdapterActionsMixin, {
  actions: {
    publish: function(type, snapshot, params) {
      // default implementation is to POST to /books/snapshot.id/publish
    }
  }
});

Custom Collection Actions

Similarly, you can create custom collection actions at the adapter or the model class layer, with just two key differences:

  1. Use Model.reopenClass to add the actions as static properties of the class, rather an a Model instance. This ensures they operate on the collection as a whole rather than a particular instance.
  2. Because they operate on a collection, your adapter won't be passed a snapshot argument. Also, your collection action handler will be passed the instance of the store, since Model classes themselves don't have a reference to the store.
// app/models/book.js
export default DS.Model.reopenClass(CollectionActionsMixin, {
  actions: {
    createWithOptions: function(adapterAction, store, params) {
      // params = the params object passed in when the action is invoked
      // Default implementation is to invoke adapterAction(params)
      //
      // Overriding this is useful if you want to do things like take a
      // response and insert a record into the store via
      // this.store.createRecord
    }
  }
});

// app/adapters/book.js or app/adapters/application.js
export default DS.RESTAdapter.extend(AdapterActionsMixin, {
  actions: {
    publish: function(type, params) {
      // default implementation is to POST to /books/publish
    }
  }
});

ember-data-actions's People

Contributors

davewasmer avatar ember-tomster avatar

Watchers

 avatar  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.