Git Product home page Git Product logo

active-redux's Introduction

Active Redux

The goal of this project is to stop bikeshedding API interactions on the frontend. We want an Active Record for Redux that works nicely with other reducers and doesn't require its own store.

This project depends on:

  • A JSON-API spec backend
  • redux and redux-thunk

Core Ideas

Resources should have models

We want to be able to interact with objects that know about their relationships with other objects, can house resource-specific information (how to read a date, how to put firstName and lastName together) and are able to persist themselves.

Models

Models are an object that read from a JSON-API resource. They define their own attributes, know their relationships and can both read and persist information.

Here's how you use models:

1. Define the model

// models/person.js
import { Attr, define } from 'active-redux';

const Person = define('people', class Person {
  static endpoints = {    // overridable API endpoints - defaults to type
    create: 'people',
    read: 'people',
    update: 'people/:id',
    delete: 'people/:id'
  };
  static attributes = {
    name: Attr.string({ default: 'Joe Schmoe '}),
    posts: Attr.hasMany('articles'), // The JSON-API type of the relation
    employer: Attr.hasOne('companies'),
  };

  get firstName() {
    return this.name.split(' ')[0] // this.name as well as other attributes are
  }                                // defined when the model is registered
});

2. Bind Active Redux to the store

// wherever the store is set
import { createStore, combineReducers } from 'redux';
import { api, bind } from 'active-redux';
import reducers from 'app/reducers';

const store = createStore(
  combineReducers({ api: api.reducer, ...reducers }),
);
bind(store);

This gives models access to the store for querying.

Use the Model

// anywhere
import Person from 'models/person';
const joe = Person.find(5)
// => Promise<Person>

Example

You can find an example here. In order to set it up, you can do the following:

cd example
yarn install

# in one process
yarn server
# in another
yarn start

active-redux's People

Contributors

berfarah avatar supernats avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

berfarah mileszim

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.