Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few helper methods to ease usage a bit. #2

Merged
merged 1 commit into from Aug 19, 2016

Conversation

dhrrgn
Copy link
Contributor

@dhrrgn dhrrgn commented Aug 19, 2016

In the real-world, you will likely have quite a few models, so reduce the boilerplate when setting up the store, I suggest 2 new helper functions:

  • combineModelReducers - Combines a set of ReduxModel reducers.
  • initModels - Initializes a set of ReduxModels.

So, a developer may create a models/index.js module which just exports all of the models:

import user from './user';
import entries from './entries';
import comments from './comments';

export {
  user,
  entries,
  comments,
};

Then in the configureStore.js, you can use the new functions like so:

import { createStore, applyMiddleware } from 'redux';
import createLogger from 'redux-logger';
import thunkMiddleware from 'redux-thunk';

// import the new helper functions
import { combineModelReducers, initModels } from 'redux-easy-models'

//models
import * as models from './models';

// combine all of the ReduxModel reducers
const reducers = combineModelReducers(models);

const logger = createLogger();
const store = createStore(
    reducers,
    applyMiddleware(...[thunkMiddleware, logger])
);

// init all the ReduxModels
initModels(models, store);

@dhrrgn
Copy link
Contributor Author

dhrrgn commented Aug 19, 2016

I just updated the functions to support both arrays of models, and an object containing models. I did this so that it actually supports the example I gave.

import * as models from './models' results in models being an object (which i somehow forgot), and it probably a common use case, so supporting it makes sense.

@dhrrgn dhrrgn force-pushed the patch-1 branch 3 times, most recently from b1b94a9 to 49309b4 Compare August 19, 2016 02:25
In the real-world, you will likely have quite a few models, so reduce the boilerplate when setting up the store, I suggest 2 new helper functions:

* `combineModelReducers` - Combines a set of ReduxModel reducers.
* `initModels` - Initializes a set of ReduxModels.

So, a developer may create a `models/index.js` module which just exports all of the models:

```javascript
import user from './user';
import entries from './entries';
import comments from './comments';

export {
  user,
  entries,
  comments,
};
```

Then in the `configureStore.js`, you can use the new functions like so:

```javascript
import { createStore, applyMiddleware } from 'redux';
import createLogger from 'redux-logger';
import thunkMiddleware from 'redux-thunk';

// import the new helpers as well
import ReduxModel, { combineModelReducers, initModels } from 'redux-easy-models'

//models
import * as models from './models';

// combine all of the ReduxModel reducers
const reducers = combineModelReducers(models);

const logger = createLogger();
const store = createStore(
    reducers,
    applyMiddleware(...[thunkMiddleware, logger])
);

// init all the ReduxModels
initModels(models, store);
```

Update the new helper methods to allow objects to be sent.

Move the iteration logic into its own function, add error handling.
@machadogj
Copy link
Owner

This looks very good! I'll try to merge and update npm tomorrow.

@machadogj machadogj merged commit 384b858 into machadogj:master Aug 19, 2016
@machadogj
Copy link
Owner

machadogj commented Aug 19, 2016

Hi @dhrrgn, I published a new version to npm, and updated the readme with your instructions, let me know if that looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants