Last active
August 23, 2016 14:02
-
-
Save danlourenco/40f078ce7d4c19504560b091647ad290 to your computer and use it in GitHub Desktop.
component interpolation within handlebars template
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: ['ember-component'] | |
}); |
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: ['ember-component'] | |
}); |
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'; | |
const { computed, get } = Ember; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
display: ['<table border="1" cellpadding="0" cellspacing="0" width="100%"><tbody>↵<tr>↵<td width="50%"><b>QUESTIONS</b></td>↵<td width="50%"><b>RESPONSES</b></td>↵</tr>↵↵<tr>↵<td width="50%">Blue is your favorite color?</td>↵<td width="50%">', | |
{componentName: "my-component"}, | |
'</td>↵</tr>↵↵<tr><td width="50%">Clowns are evil?</td><td width="50%">', | |
{componentName: "hello-component"}, | |
'</td>↵</tr>↵↵</tbody></table>↵↵<p> ' | |
], | |
compiledHTML: Ember.computed('display', function(){ | |
let display = get(this, 'display'); | |
let domString = ''; | |
display.forEach(function(el) { | |
if (typeof el === 'string') { | |
domString += el; | |
} else { | |
domString += '<span class="replace-with-component"></span>'; | |
} | |
}); | |
return domString; | |
}), | |
componentsToRender: Ember.computed('display', function() { | |
let display = Ember.get(this, 'display'); | |
let componentsToRender = []; | |
display.forEach(function(item) { | |
if (item.componentName) { | |
componentsToRender.push(item); | |
} | |
}); | |
return componentsToRender; | |
}), | |
worksheetHasTableLayout: computed('compiledHTML', function() { | |
let html = get(this, 'compiledHTML'); | |
return html.indexOf('<table') !== -1; | |
}), | |
// can we programatically render all components in componentsToRender array into DOM elements with a class of 'js-componentInsertion' ? | |
init() { | |
this._super(...arguments); | |
this.scheduleMoveComponents(); | |
}, | |
didInsertElement: function() { | |
this.scheduleMoveComponents(); | |
}, | |
scheduleMoveComponents:(function(){ | |
var self = this; | |
Ember.run.schedule('afterRender', function() { | |
self._moveComponents(); | |
}); | |
}).observes('display'), | |
_moveComponents: function() { | |
if (!Ember.get(this, 'worksheetHasTableLayout')) { | |
return; | |
} | |
let componentSlots = Ember.$('.replace-with-component'); | |
let components = Ember.$('.ember-component'); | |
componentSlots.each(function(item) { | |
Ember.$(this).replaceWith(components[item]); | |
}); | |
} | |
}); | |
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; | |
} | |
.ember-component { | |
background: lightblue; | |
border: 1px solid; | |
border-radius: 2px; | |
margin: 2px; | |
} |
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.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