Skip to content

Instantly share code, notes, and snippets.

@jlami
Last active March 22, 2019 10:47
Show Gist options
  • Save jlami/b7a3b75c969591a8375ba791105326d4 to your computer and use it in GitHub Desktop.
Save jlami/b7a3b75c969591a8375ba791105326d4 to your computer and use it in GitHub Desktop.
Array of Arrays
import Ember from 'ember';
let Container = Ember.Object.extend({
init() {
this.set('items', []);
},
comp2: Ember.computed('items.@each.j', function() {
console.log('2deep');
//console.log(this.items.mapBy('j'));
return Math.random();
}),
obs: Ember.observer('items.@each.j', function() {
//console.log(this.items.mapBy('i'));
console.log('observed2');
this.notifyPropertyChange('obs');
}),
});
let Item = Ember.Object.extend({
j: Ember.observer('i', function() {
console.log('j');
//console.log(this.i);
this.notifyPropertyChange('j');
return Math.random();
}),
});
let counter = 0;
export default Ember.Controller.extend({
init() {
let result = [];
let last = null, lastRow = null;
for (let i=0; i<5; i++) {
let row = Container.create();
for (let j=0; j<4; j++) {
let item = last = Item.create({i: j});
row.items.pushObject(item);
}
lastRow = row;
result.push(row);
}
this.set('a', result);
this.set('last', last);
this.set('lastRow', lastRow);
},
obs: Ember.observer('a.@each.compx', function() {
//console.log(this.a.mapBy('comp'));
console.log('observed');
}),
comp: Ember.computed('a.@each.obs', function() {
console.log('comp');
//console.log(this.a.mapBy('comp2'));
//console.log(this.a.mapBy('items').map(x => x.mapBy('j')));
//console.log(this.a);
return Math.random();
}),
actions: {
increment() {
//this.last.notifyPropertyChange('i');
this.lastRow.items.invoke('notifyPropertyChange', 'i');
//this.last.set('i', counter++);
//this.set('i', counter++);
},
read() {
//console.log(this.last.i);
//console.log(this.a.mapBy('comp'));
//console.log(this.lastRow.items.mapBy('i'));
},
},
});
<h1>Listener on array of arrays</h1>
<br>
<br>
{{comp}}
<button {{action 'increment'}}>Increment</button>
<button {{action 'read'}}>Read</button>
{{outlet}}
<br>
<br>
{
"version": "0.15.1",
"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.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment