Last active
August 10, 2016 02:47
-
-
Save visualjeff/50ccf9ea97f2265384723c79aa3b3b1e 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'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
yenConverter: Ember.inject.service(), | |
actions: { | |
switchCurrency(){ | |
this.get('yenConverter').switchCurrency(); | |
} | |
} | |
}); |
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'; | |
//example of a function helper. Not a class helper | |
export function formatCurrency([value, ...rest]) { | |
let dollars = Math.floor(value / 100); | |
let cents = value % 100; | |
let sign = "$"; | |
if (cents.toString().length === 1) { cents = '0' + cents; } | |
return `${sign}${dollars}.${cents}`; | |
} | |
export default Ember.Helper.helper(formatCurrency); |
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'; | |
//Hashes are useful for configuring the behavior of a helper. NOTE: I set a default value if the hash value isn't provided. | |
export function hashHelper(params, hash) { | |
let firstWord = hash.firstWord === undefined ? "Good-Bye" : hash.firstWord; | |
let secondWord = hash.secondWord == undefined ? "Friend" : hash.secondWord; | |
return `${firstWord} ${secondWord}!`; | |
} | |
export default Ember.Helper.helper(hashHelper); |
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'; | |
//parameters are good for passing values | |
export function parameterHelper(params/*, hash*/) { | |
let [ firstParam, secondParam ] = params; | |
return `${firstParam} ${secondParam}!`; | |
} | |
export default Ember.Helper.helper(parameterHelper); |
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'; | |
//Class helper. Implements compute and recompute. | |
//Uses Ember.String.html save to render html. | |
//Uses Ember.Handlebars.Utils.escapeExpression to escape values from the backend. | |
export default Ember.Helper.extend({ | |
yenConverter: Ember.inject.service(), | |
compute(params, {price}) { | |
//Needed to wrap $ and ¥ in double quotes. Singles quotes caused problems. | |
var symbol = this.get('yenConverter.isInDollars') ? "$" : "¥"; | |
var convertedPrice = this.get('yenConverter').exchange(price); | |
var roundedPrice = Math.round(convertedPrice * 100)/100; | |
return Ember.String.htmlSafe(`<b>${symbol}${Ember.Handlebars.Utils.escapeExpression(roundedPrice)}</b>`); | |
}, | |
recomputeWhenChanged: Ember.observer('yenConverter.currentCurrency', 'yenConverter.exchangeRate', function(){ | |
this.recompute(); | |
}) | |
}); |
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.Service.extend({ | |
currentCurrency: 'Dollars', | |
exchangeRate: 121.60, | |
isInDollars: Ember.computed.equal('currentCurrency', 'Dollars'), | |
exchange(dollarAmount){ | |
if(this.get('isInDollars')){ | |
return dollarAmount; | |
} else { | |
return dollarAmount * this.get('exchangeRate'); | |
} | |
}, | |
switchCurrency(){ | |
if(this.get('isInDollars')){ | |
this.set('currentCurrency', 'Yen'); | |
} else { | |
this.set('currentCurrency', 'Dollars'); | |
} | |
} | |
}); |
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.10.4", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.7.0", | |
"ember-data": "2.7.0", | |
"ember-template-compiler": "2.7.0" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment