-
Notifications
You must be signed in to change notification settings - Fork 248
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
Add items collection #1638
Add items collection #1638
Conversation
@lc-thomasberger what do you think? |
I just started to test this using a new component for which I originally used itemsModel. I understand the benefit of using a collection but now it feels a bit more laboured now as I can no longer use a single handlebars template to iterate over The issue is that
With maybe some defensive programming to ensure we can call You can see this is a simple extension of the original Backbone method here: https://github.com/jashkenas/backbone/blob/master/backbone.js#L439-L441 |
Sounds fine to me. Need anything else? |
Everything else works great! |
}, | ||
|
||
checkCompletionStatus: function() { | ||
if (this.areAllItemsCompleted()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return early?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, I think defiite overkill / needless expansion for single-line if statements. Only an issue if dealing with multiple nested lines and/or actions after the conditional.
var items = this.get('_items'); | ||
var collection = new Backbone.Collection(null, { model: ItemModel }); | ||
|
||
items.forEach(function(item, index) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could actually call .map()
here and call the Collection add()
with an array of items
after.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you write out the code? my brain has gone to sleep... :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this:
var items = items.map(function(item, index) {
item._index = index;
return item;
});
collection.add(items);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't make use of the array returned by calling .map so .forEach could still be used. If immutability is desirable though
var items = items.slice().map(
would prevent mutation
Also since there's now only one .add call why not just use
var collection = new Backbone.Collection(items, { model: itemModel })
After the array has been mapped.
Sorry for the lack of MD I'm on a phone currently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.
}, | ||
|
||
reset: function(type, force) { | ||
this.get('_items').each(function(item) { item.reset(); }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be more readable on a single line if we were using arrow functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:O * gasp *
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't do that with IE 11 still in the mix though. :-/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to have a chat with you about that kind of stuff.
could you all re-+1 this so it can go in? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job, @tomgreenfield. 👍
Original issue: #1637.
fixes:
#1365
#1178
Please test in conjuction with https://github.com/adaptlearning/adapt-contrib-accordion/tree/issue/1637.