Last active
October 16, 2015 03:57
-
-
Save arenoir/c5036b3b2c86372aed8a to your computer and use it in GitHub Desktop.
New Twiddle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
const {A, computed, isEmpty} = Ember; | |
export default Ember.Controller.extend({ | |
appName:'Ember Twiddle', | |
length: 1000, | |
list: null, | |
actions: { | |
regenerate() { | |
this.transitionToRoute('lists.show', 1); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
const {computed} = Ember; | |
export default Ember.Controller.extend({ | |
nextId: 2, | |
lists: [], | |
next: computed('lists.[]', 'model', function() { | |
let lists = this.get('lists'); | |
let model = this.get('model'); | |
let index = lists.indexOf(model); | |
if (index >= 0) { | |
return lists.objectAt(index + 1) || parseInt(model.get('id')) + 1; | |
} | |
return 2; | |
}), | |
prev: computed('lists.[]', 'model', function() { | |
let lists = this.get('lists'); | |
let model = this.get('model'); | |
let index = lists.indexOf(model); | |
if (index > 0) { | |
return lists.objectAt(index -1) || parseInt(model.get('id')) - 1; | |
} | |
return 1; | |
}), | |
push(model) { | |
let lists = this.get('lists'); | |
lists.pushObject(model); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
model(params) { | |
let list = this.store.createRecord('list', {id: params.id}); | |
list.buildItems(); | |
return list; | |
}, | |
setupController(ctlr, model) { | |
ctlr.push(model); | |
ctlr.set('model', model); | |
ctlr.set('nextId', model.get('id') + 1); | |
} | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.Model.extend({ | |
name: DS.attr('string'), | |
cost: DS.attr('number'), | |
list: DS.belongsTo('list') | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import DS from 'ember-data'; | |
const {hasMany} = DS; | |
const {RSVP, computed, isEmpty} = Ember; | |
export default DS.Model.extend({ | |
items: hasMany('item'), | |
itemsCost: computed('[email protected]', function() { | |
var items = this.get('items'); | |
if (isEmpty(items)) { | |
return 0; | |
} | |
return items.reduce(function(value, item) { | |
value += item.get('cost') || 0; | |
return value; | |
}, 0); | |
}), | |
buildItems() { | |
let store = this.store; | |
let items = this.get('items'); | |
return new RSVP.Promise((resolve, reject) => { | |
for(var i = 0; i < 1000; i++) { | |
let item = store.createRecord('item', { | |
name: 'name-' + Math.random().toString(), | |
cost: Math.random() | |
}); | |
items.pushObject(item); | |
} | |
resolve(items); | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
const {computed} = Ember; | |
export default Ember.Component.extend({ | |
sort: ['cost:asc'], | |
sorted: computed.alias('list.items', 'sort'), | |
cost: computed.alias('list.itemsCost') | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import config from './config/environment'; | |
var Router = Ember.Router.extend({ | |
location: config.locationType | |
}); | |
Router.map(function() { | |
this.route('lists', function() { | |
this.route('show', {path: ":id"}); | |
}); | |
}); | |
export default Router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.4.13", | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember.debug.js", | |
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.1.0/ember-data.js", | |
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember-template-compiler.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment