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

props, derived, session for ampersand-collection #10

Closed
dhritzkiv opened this issue Jul 22, 2014 · 9 comments
Closed

props, derived, session for ampersand-collection #10

dhritzkiv opened this issue Jul 22, 2014 · 9 comments

Comments

@dhritzkiv
Copy link
Member

Was wondering if there was a way to have properties (props, derived, session) for ampersand-collection. Would be particularly useful to have a derived properties that can return values based on the collection's array.

For example, say you have a collection called products filled with product models. Each product model has a price property. It would be nice to define a derived property on products that could map/reduce the product prices into a total property. This total would trigger event listeners bound to it (as ampersand-state does with its properties).

Is this possible?

@kamilogorek
Copy link
Contributor

Not really, but you can easily do that on your own, here's the working PoC:

https://gist.github.com/kamilogorek/df66f02c452f67170062

@dhritzkiv
Copy link
Member Author

Thanks!

On Jul 23, 2014, at 11:08 AM, Kamil Ogórek notifications@github.com wrote:

Not really, but you can easily do that on your own, here's the working PoC:

https://gist.github.com/kamilogorek/df66f02c452f67170062


Reply to this email directly or view it on GitHub.

@wraithgar
Copy link
Contributor

this is almost exactly how we've solved this problem in the past, just remember to include add and remove events in your listenTo

@HenrikJoreteg
Copy link
Member

We've talked about supporting this. Then you'd be able to directly listen to change:length for example. That seems to be the most common use case, but it's be cool to support this out of the box.

Trick is, with stuff like filtered sets is that it starts getting pretty inefficient as the collection size increases.

You might look at ampersand-sub-collection too. It's meant for this type of use, but has less docs/tests. it's simply not as polished.

@HenrikJoreteg
Copy link
Member

going to close this for now, not sure we've got enough impetus to add this.

@jrmyio
Copy link

jrmyio commented Aug 23, 2014

Currently facing a problem where I have a simple collection and I want to show the amount of items in the collection in the template. I cannot use bindings {} because the collection cannot have properties.

How would one solve this?

@yallups
Copy link

yallups commented Aug 23, 2014

I think it would be cool of a collection inherited from state.

@jrmyio
Copy link

jrmyio commented Aug 23, 2014

If there is not a simple solution for the collection/show length use case then I think something has to change. This could be limited to derived properties, but currently it seems you cannot have non-property dependencies for derived properties.

@HenrikJoreteg
Copy link
Member

One potential approach.

initialize: function () {
  this.on('add remove reset', function () {
    this.trigger('change:length', this.length); 
  }, this);
}

Then from a view, you could bind it like:

bindings: {
   'collection.length': {
      selector: '.collection-length'
   }
}

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

6 participants