-
Notifications
You must be signed in to change notification settings - Fork 297
Prefixes/Namespacing proposed syntax #159
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
Comments
I would prefer that this be kept out of the library; but that doesn't mean we can't come up with a reasonable solution. If we can re-implement a smarter camel-casing function (see #135), we can actually push the rest of the solution out to userland. const {createActions} = require('redux-actions')
// can genericize to leaves of any type, should be pulled from a utility module
const flatten = (
target,
delimiter,
flattened = {},
path = ''
) => {
function getNextPath(key) {
return path ? `${path}${delimiter}${key}` : key
}
for (const key in target) {
const nextPath = getNextPath(key)
if (typeof target[key] === 'function') {
flattened[nextPath] = target[key]
continue
}
flatten(target[key], delimiter, flattened, nextPath)
}
return flattened
}
const actions = flatten({
orders: {
getOrder: orderId => ({orderId}),
placeOrder: cartId => ({cartId}),
},
products: {
addToCart: (productId, quantity) => ({productId, quantity}),
list: query => ({query}),
},
rootLevelAction: () => ({}),
},
'/'
)
console.log(actions)
console.log(createActions(actions)) [~/code/private/js-standard] (master|%=)
$ node test.js
{ 'orders/getOrder': [Function: getOrder],
'orders/placeOrder': [Function: placeOrder],
'products/addToCart': [Function: addToCart],
'products/list': [Function: list],
rootLevelAction: [Function: rootLevelAction] }
{ ordersGetOrder: { [Function: actionCreator] toString: [Function] },
ordersPlaceOrder: { [Function: actionCreator] toString: [Function] },
productsAddToCart: { [Function: actionCreator] toString: [Function] },
productsList: { [Function: actionCreator] toString: [Function] },
rootLevelAction: { [Function: actionCreator] toString: [Function] } } Unfortunately our camel-casing function doesn't respect special delimiters, like |
I've released redux-nested-actions, but it won't work until #166 gets merged. This would let you namespace and structure your actions hierarchy to whatever depth you want. |
I'm using a custom solution that works for me, but it would be better if something "official" was supported. Are you going to support something like this or userland custom solutions are good enough? |
I'd like to get this into core; userland code like what I linked to above is a bit awkward. I'll submit a PR this weekend. |
I'm proposing something like https://github.com/yangmillstheory/redux-nested-actions/blob/master/src/index.test.js; planning on getting this in tomorrow. |
Thanks @yangmillstheory, it's working great. I had to change some dumb actions that were in the string form (second createActions arg) to the object form but, after all, I think it was confusing to have these mixed configs. |
For namespacing, which I would like to see, how about supporting this kind of syntax? This actually reduces boilerplate in the destructing assignment for all 2'nd level action creators and prevents having to keep keys and their destructed names in sync.
Example usage:
Destructures to the following:
Supporting an unlimited level of literal nesting would be awesome and allow for richly namespaced action creators that reduce the chance of collisions.
The text was updated successfully, but these errors were encountered: