Ember loading templates by JSON
Loading and assigning Handlebars Templates via JSON
by amindunited
HTML
<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js"></script>
<script src="http://cloud.github.com/downloads/emberjs/ember.js/ember-latest.js"></script>
<div id="appWrap">
</div>
JavaScript
////////////////////////////////////////////////////////////////////////////////////////////////
// SOF Template declarations
var Templates = [
{
name:'home',//the reference name that app will use to call the template
assignTo:'ApplicationView',//where the template will be assigned
template:'<h1>Welcome to {{title}}</h1>'//the content of the template
}
];
// EOF Template declarations
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// SOF Ember Application creation
$(function(){
window.basicApp = Ember.Application.create({
autoinit:false,//If the app auto inits the dom/data might not be ready for it (we'll call it later ourselves)
rootElement:'#appWrap'//We want to lock the app down to a specific dom element to prevent conflict with old HTML (default is 'body')
});
basicApp.ApplicationController = Em.Controller.extend();
basicApp.ApplicationView = Em.View.extend({});
basicApp.router = Em.Router.create({
enableLogging : true,
root: Em.Route.extend({
index: Em.Route.extend({
route:'/',
enter:function(){
console.log("entered root")
}
})
})
});
//basicApp.initialize(basicApp.router);//we'll call this after the template ajax request
});
// EOF Ember Application creation
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// SOF Template request
$(function(){
var ajaxRequestData = {
json: JSON.stringify(Templates),
delay: 2
}
$.ajax({
//post the request to /echo/json/ and specify the correct datatype
url: '/echo/json/',
dataType: 'json',
...