1.0.1 • Published 6 years ago

resa-class-model v1.0.1

Weekly downloads
20
License
MIT
Repository
github
Last release
6 years ago

resa-class-model

NPM version Build Status Coverage Status

ES / TypeScript decorator for class-style resa model.

Example

// App.tsx
import AppModel from './AppModel';
import { connect } from 'resa';
import { wapper } from 'resa-class-model';

interface AppProps {
  count: number;
  appModel: AppModel;
}

class App extends React.Component<AppProps> {
  render() {
    return (
      <div className="App">
          <h1>{this.props.count}</h1>
          <button onClick={() => this.props.appModel.add(1)}>+</button>
          <button onClick={() => this.props.appModel.minus(1)}>-</button>
          <button
            onClick={
              () => this.props.appModel.addAsync(2)}
          >async
          </button>
          <button
            onClick={
              () => wapper(this.props.appModel.addAsync(2)).then(() => { alert('callback'); })}
          >promise
          </button>
      </div>
    );
  }
}

const mapStateToProps = (app, state) => ({
  count: app.appModel.state,
});

const NewApp = connect(mapStateToProps, ['appModel'], null)(App);
// AppModel.ts
import { Model, reducer, init, effect } from 'resa-class-model';
import { delay } from 'redux-saga';

@init({
    name: 'appModel',
    state: 0
})
export default class AppModel extends Model {
    @effect()
    * addAsync(count: number) {
        yield delay(2000);
        this.add(count);
    }

    @reducer()
    add(count: number) {
        return this.state +  count;
    }

    @reducer()
    minus(count: number) {
        return this.state -  count;
    }
}
// index.ts
import createResa, { Provider } from 'resa';

import App from './App';
import AppModel from './AppModel';

const app = createResa();
app.registerModel(new AppModel());

ReactDOM.render(
  <Provider store={app.store} resa={app}>
    <App />
  </Provider>,
  document.getElementById('root') as HTMLElement
);

IntelliSense

In model

GitHub Logo

In view

GitHub Logo

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-rc.3

6 years ago

1.0.0-rc.2

6 years ago

1.0.0-rc.1

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago