Skip to content

Instantly share code, notes, and snippets.

@grapho
Last active March 7, 2016 15:25
Show Gist options
  • Save grapho/d3416c6d6101fff593e9 to your computer and use it in GitHub Desktop.
Save grapho/d3416c6d6101fff593e9 to your computer and use it in GitHub Desktop.
Component Grid
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'a Grid of Components',
actions: {
action1() { alert("action1 fired"); },
action2() { alert("action2 fired"); },
action3() { alert("action3 fired"); },
action4() { alert("action4 fired"); },
action5() { alert("action5 fired"); },
action6() { alert("action6 fired"); },
action7() { alert("action7 fired"); },
action8() { alert("action8 fired"); }
}
});
<h1>Welcome to {{appName}}</h1>
<p>pass in an array of components.. and set how many rows and how many cells.</p>
<p>TODO: Make sure extra rows and cells are not actually rendered.. if not enough components to fill them.. </p>
{{component-grid rows=4 cells=3
components=(array
(component 'foo-a' click=(action 'action1') fooText="1")
(component 'foo-b' click=(action 'action2') fooText="2")
(component 'foo-b' click=(action 'action3') fooText="3")
(component 'foo-b' click=(action 'action4') fooText="4")
(component 'foo-a' click=(action 'action5') fooText="5")
(component 'foo-a' click=(action 'action6') fooText="6")
(component 'foo-a' click=(action 'action7') fooText="7")
(component 'foo-a' click=(action 'action8') fooText="8")
)
}}
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['component-grid'],
gridRows: Ember.computed('components.[]', 'rows', 'cells', {
get() {
let components = this.get('components');
let totalRows = parseInt(this.get('rows'));
let totalCells = parseInt(this.get('cells'));
let rows = [];
for (let i = 0; i < totalRows; i++) {
let cells = [];
for (let j = 0; j < totalCells; j++) {
let idx = (totalCells * i) + j;
cells.push(idx);
}
rows.pushObject(Ember.Object.create({
gridCells: components.objectsAt(cells)
}));
}
return rows;
}
})
});
{{#each gridRows as |row|}}
<div class="grid-row">
{{#each row.gridCells as |cell|}}
<div class="grid-cell">
{{component cell}}
</div>
{{/each}}
</div>
{{/each}}
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'button',
classNames: ['foo-a']
});
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'button',
classNames: ['foo-b']
});
import Ember from 'ember';
export function array(params) {
return params;
}
export default Ember.Helper.helper(array);
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.component-grid {
display: table;
border-collapse: collapse;
}
.grid-row {
display: table-row;
}
.grid-cell {
display: table-cell
}
.foo-a {
display: block;
background-color: #666;
color: #fff;
width: 50px;
height: 50px;
}
.foo-b {
display: block;
background-color: #333;
color: #fff;
width: 50px;
height: 50px;
}
{
"version": "0.6.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.3.1/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.3.3/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment