Skip to content

Instantly share code, notes, and snippets.

@e00dan
Last active January 26, 2016 16:39
Show Gist options
  • Save e00dan/2bb116f7fcbef593d115 to your computer and use it in GitHub Desktop.
Save e00dan/2bb116f7fcbef593d115 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
model: Object.create({
items: [
{ item_type: 'bid' },
{ item_type: 'bid' },
{ item_type: 'donation' },
{ item_type: 'ticket' },
]
}),
createArrays: Ember.on('init', Ember.observer('[email protected]_type', function() {
console.log('observer fired');
const items = this.get('model.items'),
itemsArray = [],
donationsArray = [],
rafflesArray = [],
ticketsArray = [];
items.forEach(item => {
switch (Ember.get(item, 'item_type')) {
case 'bid':
itemsArray.pushObject(item);
break;
case 'donation':
donationsArray.pushObject(item);
break;
case 'ticket':
ticketsArray.pushObject(item);
break;
default:
rafflesArray.pushObject(item);
break;
}
});
this.setProperties({
itemsArray,
donationsArray,
ticketsArray,
rafflesArray
});
})),
actions: {
add(type) {
console.time('a');
this.get('model.items').pushObject({
item_type: type
});
console.log('after pushobject');
console.timeEnd('a');
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
Items array: <br/>
{{#each itemsArray as |item|}}
{{item.item_type}}
{{/each}}
<hr/>
Donation array: <br/>
{{#each donationsArray as |item|}}
{{item.item_type}}
{{/each}}
<hr/>
Tickets array: <br/>
{{#each ticketsArray as |item|}}
{{item.item_type}}
{{/each}}
<hr/>
Raffles array: <br/>
{{#each rafflesArray as |item|}}
{{item.item_type}}
{{/each}}
<br>
<br>
<button {{action 'add' 'bid'}}>Add item</button><button {{action 'add' 'donation'}}>Add donation</button><button {{action 'add' 'ticket'}}>Add ticket</button><button {{action 'add' 'raffle'}}>Add raffle</button>
{
"version": "0.5.0",
"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.2.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/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