Skip to content

Instantly share code, notes, and snippets.

@whichsteveyp
Created December 23, 2015 19:49
Show Gist options
  • Save whichsteveyp/120d58d0e8c1b594936e to your computer and use it in GitHub Desktop.
Save whichsteveyp/120d58d0e8c1b594936e to your computer and use it in GitHub Desktop.
// const AppDispatcher =, etc...
module.exports = {
getCommentsForRequestWithBind(requestID, rowBusinessKey) {
superagent.get(`/api/${requestID}/comments`, this.updateRequestCommentsInRow.bind(this, requestID, rowBusinessKey), someGlobalErrorAction);
},
getCommentsForRequestLambda(requestID, rowBusinessKey) {
superagent.get(`/api/${requestID}/comments`, comments => this.updatedRequestCommentsInRow(requestID, rowBusinessKey, comments), someGlobalErrorAction);
},
updateRequestCommentsInRow(requestID, rowBusinessKey, comments) {
// current data structure limits require the business key in order to look up the request and attach comments to it
/*
{
businessKey: {
requests: [
{ requestID, comments }
]
}
}
*/
AppDispatcher.dispatch({ // grabs a request in a given row, updates the comments, other magic, etc
rowBusinessKey,
requestID,
comments,
type: UPDATE_REQUEST_COMMENTS
});
}
}
@jameswomack
Copy link

Each method contains requestID, which, w/ the use of bind, indicates an alternate approach. To me, it's clear that requestID should be passed into a wrapper function. Also, I'm unsure why a this is required instead of going this-less.

const { get } = require('superagent')
const AppDispatcher = require('../dispatchers/app')
const { UPDATE_REQUEST_COMMENTS } = require('../constants/request')

module.exports = (requestID) => {
    const commentsURIPath = `/api/${requestID}/comments`

    const getCommentsForRequestLambda => rowBusinessKey =>
            get(commentsURIPath, comments => updatedRequestCommentsInRow(rowBusinessKey, comments), someGlobalErrorAction);

    const updateRequestCommentsInRow = (rowBusinessKey, comments) =>
        AppDispatcher.dispatch({
            rowBusinessKey,
            requestID,
            comments,
            type: UPDATE_REQUEST_COMMENTS
        })

    return { 
        updateRequestCommentsInRow, 
        getCommentsForRequestLambda
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment