Skip to content

Best practices for nested models #139

Open
@manuelmazzuola

Description

@manuelmazzuola

Hello, me again.
I'm wondering which are the best practices when there are nested models, for example.

I've an action like this

load: function() {
      this.dispatch(Constants.LOAD_PRODUCT)

      request
        .get(BASE_URL + '/product')
        .send((err, response) => {
          this.dispatch(Constants.LOAD_PRODUCT_SUCCESS, {products: response.body});
        })
    }

the dispatched products is a list of product

Product:
  id: 1
  name: "foo"
  comments: ["comment/1", "comment/2"]

Now, I've to fetch for each product the comments.
How? I think that this is really atrocious:

function retrieveComments(product) {
  let comments = []
  _.each(product.comments, (url) => {
    request
        .get(BASE_URL + '/' + url)
        .send((err, response) => {
          comments.push(response.body)
        })
  }

  product.comments = comments
}

load: function() {
      this.dispatch(Constants.LOAD_PRODUCT)

      request
        .get(BASE_URL + '/product')
        .send((err, response) => {
          _.each(response.body, retrieveComments)
          this.dispatch(Constants.LOAD_PRODUCT_SUCCESS, {products: response.body});
        })
    }

So how can I handle nested models with flux ?
I need to define a CommentStore ? Than I trigger the loadComment event fomr the ProductStore and waitFor it before dispatch the LOAD_PRODUCT_SUCCESS ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions