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

Calling a Getter with parameters #14

Closed
ccapndave opened this issue Feb 18, 2015 · 4 comments
Closed

Calling a Getter with parameters #14

ccapndave opened this issue Feb 18, 2015 · 4 comments

Comments

@ccapndave
Copy link

Is there a solution for this use case?

@jordangarcia
Copy link
Contributor

If a getter needs to be dynamic then it should be composed of another getter that represents the parameter as state in the system.

For example, imagine we have a store mantaining the state of a map of id => ListItem, and a store maintaining the currently selected item id.

var selectedItemGetter = [
  ['listItems'],
  ['selectedItemId'],
  function(items, id) {
    return items.get(id)
  }
]

The idea of using getters is it provides a unit of immediate evaluation for some state in the system. Another way to think about a Getter is it represents a question, in our case "What is the currently selected item?" We can get the answer to that question immediately via flux.evaluate(selectedItemGetter) or get notified whenever the answer changes via flux.observe(selectedItemGetter, handlerFn)

If you want to create a function that evaluates a getter based on the parameters it would look like:

function getItem(id) {
  return flux.evaluate([
    ['items'],
    function(items) { 
      return items.get(id)
    }
  ])
}

@loganlinn
Copy link
Contributor

Could you just use a function to create a getter? I suppose the parameter would reasonably need to be constant for the lifetime of the component where getter is bound

function getItem(id) {
  return [
    ['items'],
    function(items) { 
      return items.get(id)
    }
  ]
}

@dkozma
Copy link

dkozma commented Aug 5, 2015

@jordangarcia In your getItem() example, where do you imagine that living? In actions.js? It seems like a fundamental requirement of a getter is that it is an array, so even though this is technically gets data, it seems like it cannot go there since it doesn't fit into the prescribed pattern. The only other place I can see it logically going is actions, (or even maybe index), however I wanted to get your perspective. Thanks.

@balloob
Copy link
Contributor

balloob commented Aug 5, 2015

It should live in your getters.js as it is related to getters.

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

No branches or pull requests

5 participants