Want to find out the before and after values of the observed property in Ember.js? Use willInsertElement to store the initial value, and upon change of the value, compare the two:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Quotes.itemRowView = Ember.View.extend({ tagName: 'tr', initialValue: null, willInsertElement: function(){ var value = this.get('content').value; this.set('initialValue', value); }, valueDidChange: function() { // only run if updating a value already in the DOM if(this.get('state') === 'inDOM') { var new_value = this.get('content').value; // decreased or increased? var color = (new_value > this.get('initialValue') ) ? 'green' : 'red' ; // store the new value this.set('initialValue', new_value); // only update the value element color Ember.$(this.get('element')).find('.quote-value').css('color', color); } }.observes('content.value') }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.