Skip to content

Instantly share code, notes, and snippets.

@runspired
Last active August 25, 2019 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save runspired/a7ad6d451ee880400e3028f47ba67606 to your computer and use it in GitHub Desktop.
Save runspired/a7ad6d451ee880400e3028f47ba67606 to your computer and use it in GitHub Desktop.
EachChunk
import Ember from 'ember';
const { Component } = Ember;
export default class EachChunk extends Component {};
EachChunk.tagName = '';
EachChunk.reopenClass({
positionalParams: ['items', 'chunkSize']
});
import Ember from 'ember';
const { Component } = Ember;
export default class SimpleElement extends Component {};
SimpleElement.reopenClass({
positionalParams: ['tagName']
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
items: [
'Chris', '@runspired', 'James', 'Wesley', 'Zach', 'Jill', 'Tanya', 'Renee', 'Rosemary', 'Thomas', 'John'
]
});
import Ember from 'ember';
export function chunk(params/*, hash*/) {
let [items, chunkSize] = params;
let chunks = [];
for (let i = 0; i < items.length; i++) {
let chunk = [];
chunks.push(chunk);
for (let j = 0; j < chunkSize && i < items.length; j++, i++) {
chunk.push({
item: items[i],
index: i
});
}
}
return chunks;
}
export default Ember.Helper.helper(chunk);
<h1>Chunking Lists (for grids)</h1>
<br>
<table>
<tbody>
{{#with (component 'simple-element' 'tr') as |row|}}
{{#each-chunk items 3 chunkContainer=row as |item index row col|}}
<td>{{row}}:{{col}} <b>{{item}}</b> <i>item {{index}}</i></td>
{{/each-chunk}}
{{/with}}
</tbody>
</table>
{{#each (chunk items chunkSize) as |chunk chunkIndex|}}
{{#component chunkContainer index=chunkIndex}}
{{#each chunk as |chunkItem chunkPosition|}}
{{yield chunkItem.item chunkItem.index chunkIndex chunkPosition}}
{{/each}}
{{/component}}
{{/each}}
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment