Comments (13)
One other option would be a mixin, however it would require the end-user to initialize the mixin code as part of the collection's setup.
from ampersand-collection.
I like the sound of a state object on the collection. I think this could be really useful. 👍
from ampersand-collection.
dont add remove
imply a length
change? maybe the events just need to pass additional meta?
from ampersand-collection.
@cdaringe reset changes length too
from ampersand-collection.
on add remove
events, could you just check the passed model's modelChanaged.collection.length
(if .collection
is set), and on reset
assume length 0? actually, this wouldn't work when a model is shared across collections.
new idea. perhaps we pass more than just the model to event handler--pass a {collection: collection, model: model}
hash as the context. this would break a lot of code :/
from ampersand-collection.
@wraithgar Before diving in, I'm guessing there's some history and discussion of why ampersand-collection
doesn't just inherit from ampersand-state
like everything else? (Guessing this is because of API conflicts like get
)
from ampersand-collection.
I've discussed this a bunch with @HenrikJoreteg and others in the past, and even got as far as getting something working in a branch: https://github.com/AmpersandJS/ampersand-collection/compare/based-on-state
The last time I thought about this where I think got to was:
- Ampersand-collection would inherit from state.
models
would effectively be a property of the collection, changes to the models array (add remove reset etc) would trigger achange:models
event on the collection.length
is then a simple derived property of models- you could then easily define other derived props on a collection (like totalling values on models in the collection easily)
The outstanding questions were:
- as @imlucas pointed out: state has
get/set
methods and so does collection. These do different things, and both would potentially be required if we allowed a collection to haveprops/session
attributes too (in that branch I just renamed it collSet to get something working. - If you just rethrow individual model events from the collection as is, e.g.
change:name
then you now have no way of knowing if that's a change on an item in the collection, or a change on the collection itself. I think my plan was to rethrow model events as, e.g.change:models.name
. But that's obviously a fairly significant change to how things work currently.
from ampersand-collection.
I'm trying to wrap my head around this whole situation and it seems to me that extending from state adds a lot of extra stuff to collection that we don't really need, also it seems like having a state object ask whether it has actually become a collection is a bad programming practice (I can't explain why, and I could very well be wrong!).
What about if we had another dataType in state which was 'collection' instead of having collections as its own separate thing. That way you could have deps refer to a property of type 'collection' to know when to recalculate and cache the derived property? Could it work like an array? It just bubbles up a change when something in the collection changes?
from ampersand-collection.
@latentflip thanks! Still chewing on this a bit. Here's an example (using a gnarly "what if there was an internal _state that proxied?" method) which is what first got me into this topic.
from ampersand-collection.
@latentflip the direction your branch is heading seems promising. in my use-case, a collection has to have information about pagination, sorting and verbose filtering of items - so extending from ampersand-state
would help me out a lot.
At the moment, i'm simply adding additional Models as properties of the collection.
from ampersand-collection.
+1 on extending &-state.
the change:models.name
way sounds good too
from ampersand-collection.
+1 on adding properties to collection. A lot of boilerplate is generated without it. However, I also agree with @erd0s, seems like by extending from state we'd be inviting a whole lot of weirdness. Collections could then have collections
, or children
as an example, and would seem to lose their purpose.
Perhaps we would want to lift only certain parts out of state, props
, and derived
props stand out. I realize how much code that actually means bringing over though.
@latentflip, I noticed that in your branch you have a deps
on area
for the collection, but I suspect it doesn't actually respond to individual changes on area
for models. I only notice because this is the exact same mistake my team and I make all the time with derived properties. They may mean nothing but the code just silently ignores them.
Would 99% of desire-ments for this be solved with an evented property for length
and a tutorial for people on defining their own properties on collections like @imlucas has done or just using composition as @gonsfx is doing?
My two cents...
from ampersand-collection.
I just realized I could be wrong about my comment about the code not responding to change of model area
events. I forgot derived events trigger off of 'change:property' and that's exactly what collections trigger. Sorry about that @latentflip!
from ampersand-collection.
Related Issues (20)
- Fix performance issues in collections and subcollections HOT 10
- deIndex is being called wrong HOT 8
- -nothing-to-see-here- HOT 1
- .get(id) can't find object with id 0 HOT 2
- .find(query, [indexName]) to retrieve multiple models HOT 4
- using ES5 `bind` for double argument comparator causes Phantom.js to fail HOT 2
- adding element with id:0 results in duplicates
- Upgrade from 1.4.2 to 1.4.3 causes jasmine tests to fail HOT 3
- Consistent sorting functions HOT 4
- Collection of Collections HOT 3
- index issue during collection.set
- Iterable via for...of HOT 1
- Document `parse` method HOT 1
- Collection does not sort itself when elements change HOT 4
- No good hook for when models are inserted
- No "update" event like in Backbone
- lodash helper do not support second parameter binding HOT 1
- Feature Proposal: "cancel" callback in fetch options? HOT 1
- When null is passed as value, it instantiates an empty model in the collection HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ampersand-collection.