Created
September 8, 2013 20:43
-
-
Save kennethdavidbuck/6488251 to your computer and use it in GitHub Desktop.
dynamic ember app creation
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
/* | |
* Application Initialization | |
*/ | |
App.initializer({ | |
name: 'bootstrap', | |
initialize: function(container, application) { | |
"use strict"; | |
App.deferReadiness(); | |
var store = DS.get('defaultStore'), | |
databaseTables = [], | |
plurals = {}, | |
databaseTable; | |
// Populate data | |
$.get('api/bootstrap', function(data) { | |
// Build rest of app. | |
data.tables.forEach(function(table){ | |
// add specialized plurals | |
plurals[table.singular] = table.plural; | |
// store database table for reference | |
var meta = {}, irregularPropertyKeys = {},modelName = table.model_name; | |
var appModelName = 'App.%@'.fmt(modelName); | |
for(var i=0;i<table.properties.length;i++) { | |
var property = table.properties[i]; | |
// ember data uses id by default so we don't | |
// need to add it. | |
if(property.original_property.toLowerCase() !== 'id') { | |
// add property & map | |
var fullModelName ='App.%@'.fmt(modelName); | |
var emberProperty = property.ember_property; | |
var originalProperty = property.original_property; | |
meta[emberProperty] = DS.attr(property.javascript_type, { | |
defaultValue:property.default_value | |
}); | |
var mapped = {}; | |
mapped[emberProperty] = {key:originalProperty}; | |
DS.RESTAdapter.map(fullModelName,mapped); | |
} | |
} | |
// | |
table.belongsTo.forEach(function(belonging,index){ | |
// add association | |
var field = belonging.property; | |
var model = belonging.model; | |
// add association | |
meta[field] = DS.belongsTo('App.%@'.fmt(model)); | |
},this); | |
// | |
table.hasMany.forEach(function(has,index){ | |
var field = has.property; | |
var model = has.model; | |
meta[field] = DS.hasMany('App.%@'.fmt(model)); | |
}); | |
// | |
App[modelName] = DS.Model.extend(meta); | |
// used for controller and route names | |
var baseName = table.base_name; | |
/** | |
* Dynamically create routes | |
*/ | |
App.Router.map(function(){ | |
this.resource('browse',function(){ | |
this.resource(table.plural,function(){ | |
this.route('add'); | |
this.route('query',{ path:'/:query_string' }); | |
}); | |
}); | |
}); | |
/** | |
* Build Root Route | |
*/ | |
var routeName = '%@Route'.fmt(baseName); | |
App[routeName] = Ember.Route.extend(); | |
/** | |
* Build Index Controller | |
*/ | |
var controllerName = '%@IndexController'.fmt(baseName); | |
App[controllerName] = App.PaceTable.ReflectionController.extend({ | |
currentModel:App[modelName] | |
}); | |
/** | |
* Build Query Controller | |
*/ | |
controllerName = '%@QueryController'.fmt(baseName); | |
App[controllerName] = App.PaceTable.ReflectionController.extend({ | |
currentModel:App[modelName] | |
}); | |
/** | |
* Build Index Route | |
*/ | |
routeName = '%@IndexRoute'.fmt(baseName); | |
App[routeName] = Ember.Route.extend({ | |
// | |
beforeModel:function(){ | |
var tableId = table.id; | |
var currentTable = App.PaceTable.find(tableId); | |
this.controllerFor('browse').set('tableSelection',currentTable); | |
}, | |
// | |
model: function(params) { | |
return App[modelName].find(); | |
}, | |
// | |
afterModel:function(model){}, | |
// | |
setupController:function(controller,model){ | |
controller.set('model',model); | |
}, | |
// | |
renderTemplate:function() { | |
this.render('pace-table'); | |
} | |
}); | |
/** | |
* Build Table Query Route | |
* (extend standard route and modify for query). | |
*/ | |
routeName = '%@QueryRoute'.fmt(baseName); | |
App[routeName] = Ember.Route.extend({ | |
// | |
beforeModel:function(){}, | |
// | |
model:function(params) { | |
var queryString = params.query_string; | |
return DS.defaultStore.findQuery(App[modelName],queryString); | |
}, | |
// | |
afterModel:function(){}, | |
// | |
setupController:function(controller,model){ | |
controller.set('content',model); | |
}, | |
// | |
renderTemplate:function() { | |
this.render('pace-table'); | |
} | |
}); | |
}); | |
// defined pluralized valued in or to be consistent | |
// with the server. | |
DS.RESTAdapter.configure("plurals",plurals); | |
container.lookup('controller:adminTables').set('content',App.PaceTable.find()); | |
container.lookup('controller:browse').set('content',App.PaceTable.find()); | |
App.advanceReadiness(); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment