Last active
February 24, 2016 20:38
-
-
Save Naxum/423227c316040f8223ef to your computer and use it in GitHub Desktop.
Arrays not updating when elements are modified
This file contains 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.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
This file contains 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({ | |
title: 'hey', | |
box: [], | |
otherBox: [] | |
}); |
This file contains 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({ | |
classNames: ['box-item'], | |
title: Ember.computed('boxItem.title', function() { | |
return this.get('boxItem.title'); | |
}), | |
sku: Ember.computed('boxItem.sku', function() { | |
return this.get('boxItem.sku'); | |
}), | |
quantity: Ember.computed('boxItem.quantity', function() { | |
return this.get('boxItem.quantity'); | |
}) | |
}); |
This file contains 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'; | |
let BoxItem = Ember.Object.extend({ | |
title: 'title', | |
sku: 'sku', | |
quantity: 1 | |
}); | |
export default Ember.Component.extend({ | |
boxOne: [ | |
BoxItem.create({ title:'Sweatshirt', sku:'ss-01', quantity: 1}), | |
BoxItem.create({ title:'Jeans', sku:'je-01', quantity: 7}) | |
], | |
boxTwo: [], | |
actions: { | |
moveSome(boxItem, fromBox, destinationBox, amount) { | |
var existing = destinationBox.findBy('sku', boxItem.get('sku')); | |
if(existing != null) { | |
//if an item with the same sku exists in te destination box, modify its quantity | |
existing.set('quantity', existing.get('quantity') + amount); | |
Ember.Logger.info('Setting pre-existing boxItem in desinationBox to quantity:', existing.get('quantity')); | |
} else { | |
//otherwise make a new item | |
destinationBox.pushObject(BoxItem.create({ | |
title: boxItem.get('title'), | |
sku: boxItem.get('sku'), | |
quantity: amount | |
})); | |
Ember.Logger.info('Pushing new item to destinationBox with quantity:', amount); | |
} | |
if(boxItem.get('quantity') <= amount) { | |
//if we moved all we have, remove the old item | |
Ember.Logger.info('Removing old item now that its out of quantity.', boxItem); | |
fromBox.removeObject(boxItem); | |
} else { | |
//just remove some quantity | |
Ember.Logger.info('Reducing quantity of', boxItem, 'by amount', amount); | |
boxItem.set('quantity', boxItem.get('quantity') - amount); | |
} | |
} | |
} | |
}); |
This file contains 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
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
.box-item { | |
padding: 1em; | |
background: #EEE; | |
margin-bottom: 2em; | |
} | |
.box-item .controls { | |
margin-top: 1em; | |
} |
This file contains 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.6.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.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