Skip to content

Instantly share code, notes, and snippets.

@pichfl
Last active April 27, 2017 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pichfl/e30557a60c13d75ba789532e636a5b49 to your computer and use it in GitHub Desktop.
Save pichfl/e30557a60c13d75ba789532e636a5b49 to your computer and use it in GitHub Desktop.
Button Interaction inside translation
import Ember from 'ember';
var htmlEscapes = [
{
char: /&/gim,
repl: '&',
},
{
char: /</gim,
repl: '&lt;',
},
{
char: />/gim,
repl: '&gt;',
},
{
char: /"/gim,
repl: '&quot;',
},
{
char: /'/gim,
repl: '&#39;'
}
];
function escapeHTML(str) {
return htmlEscapes.reduce((acc, { char, repl }) =>
acc.replace(char, repl), str);
}
export default Ember.Component.extend({
translation: '',
text: Ember.computed('translation', function () {
let translation = this.get('translation');
// Escape HTML (if you have lodash, use _.escape)
translation = escapeHTML(translation);
// Unescape only the button
translation = translation
.replace(/&lt;(\/?)button&gt;/gi, '<$1button>');
return Ember.String.htmlSafe(translation);
}),
onButtonClick: () => {},
click(event) {
if (event.target.tagName.toLowerCase() === 'button') {
console.log('Clicked button, do something');
this.get('onButtonClick')();
} else {
console.log('clicked elsewhere, ignore');
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
translation: `A long translation that contains <button>a button</button> that should be interactive`,
actions: {
showThem() {
window.alert('Hellooooo');
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{a-button translation=translation onButtonClick=(action 'showThem')}}
{{outlet}}
<br>
<br>
{
"version": "0.12.1",
"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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment