├── .npmignore
├── .gitignore
├── test
└── index.js
├── README.md
├── package.json
└── src
└── resolver.js
/.npmignore:
--------------------------------------------------------------------------------
1 | src
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | lib
3 |
--------------------------------------------------------------------------------
/test/index.js:
--------------------------------------------------------------------------------
1 | import connectResolver, { Resolver } from '../'
2 | import React from 'react'
3 | import ReactDomServer from 'react-dom/server'
4 | import assert from 'assert'
5 |
6 | class Test extends React.Component {
7 | render() {
8 | return
{this.props.world}
9 | }
10 | }
11 |
12 | const ConnectedTest = connectResolver(Test, {
13 | resolve() {
14 | return {
15 | world: Promise.resolve('hello world')
16 | }
17 | }
18 | })
19 |
20 | Resolver.resolve(() => ).then(({ data, Resolved }) => {
21 | assert(data['.0.0'].world === 'hello world')
22 | assert(/hello world/.test(ReactDomServer.renderToStaticMarkup()))
23 | })
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # alt-resolver
2 |
3 | > A [connect to stores](https://github.com/altjs/react) that works with [react-resolver](https://github.com/ericclemmons/react-resolver)
4 |
5 | Example
6 |
7 | ```js
8 | import { resolver } from 'alt-resolver'
9 | import React from 'react'
10 | import UserStore from '../stores/UserStore'
11 |
12 | class MyComponent extends React.Component {
13 | render() {
14 | return Hello, {this.props.userName}!
15 | }
16 | }
17 |
18 | resolver(MyComponent, {
19 | listenTo() {
20 | return [UserStore]
21 | },
22 |
23 | resolve() {
24 | return {
25 | user: Promise.resolve({ name: 'Jane' }),
26 | }
27 | },
28 |
29 | getProps(props) {
30 | return {
31 | userName: props.name,
32 | }
33 | },
34 | })
35 | ```
36 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "alt-resolver",
3 | "version": "0.1.0",
4 | "description": "A connect to stores that works with react resolver",
5 | "main": "lib/resolver.js",
6 | "scripts": {
7 | "build": "npm run clean && npm run transpile",
8 | "clean": "rimraf lib",
9 | "prepublish": "npm run build",
10 | "pretest": "npm run clean && npm run transpile",
11 | "test": "babel-node test/index.js",
12 | "transpile": "babel src --out-dir lib --stage 0"
13 | },
14 | "repository": {
15 | "type": "git",
16 | "url": "git@github.com:altjs/resolver.git"
17 | },
18 | "dependencies": {
19 | "alt-react": "0.0.1",
20 | "react-resolver": "2.0.3"
21 | },
22 | "keywords": [
23 | "resolver",
24 | "async",
25 | "resolve",
26 | "isomorphic",
27 | "flux",
28 | "dispatch",
29 | "connect",
30 | "react",
31 | "stores"
32 | ],
33 | "author": "Josh Perez ",
34 | "license": "MIT",
35 | "bugs": {
36 | "url": "https://github.com/altjs/resolver/issues"
37 | },
38 | "homepage": "https://github.com/altjs/resolver",
39 | "devDependencies": {
40 | "react": "0.14.0",
41 | "react-dom": "0.14.0",
42 | "rimraf": "2.4.3"
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/resolver.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Resolver } from 'react-resolver'
3 | import ConnectBase from 'alt-react/ConnectBase'
4 |
5 | const toResolverProps = (resolver) => {
6 | return Object.keys(resolver).reduce((props, prop) => {
7 | props[prop] = () => resolver[prop]
8 | return props
9 | }, {})
10 | }
11 |
12 | export { Resolver }
13 |
14 | export default (Component, spec) => {
15 | return class extends ConnectBase {
16 | static displayName = `Resolver${Component.displayName || Component.name}Container`
17 |
18 | constructor(props, context) {
19 | super(props, context)
20 |
21 | this.setConnections(props, context, spec)
22 |
23 | this.state = this.resolveState(props)
24 | }
25 |
26 | resolveState(nextProps) {
27 | const resolve = this.config.resolve
28 | ? toResolverProps(this.call(this.config.resolve, nextProps), nextProps)
29 | : {}
30 | return { resolve }
31 | }
32 |
33 | componentWillReceiveProps(nextProps) {
34 | if (this.config.willReceiveProps) {
35 | this.call(this.config.willReceiveProps, nextProps)
36 | }
37 | this.setState(this.resolveState(nextProps))
38 | }
39 |
40 | storeDidChange() {
41 | this.setState(this.resolveState(this.props))
42 | }
43 |
44 | render() {
45 | return (
46 |
47 | {resolved => {
48 | const props = this.getNextProps({
49 | ...resolved,
50 | ...this.props,
51 | })
52 | return (
53 |
58 | )
59 | }}
60 |
61 | )
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------