Skip to content

Instantly share code, notes, and snippets.

@walter
Created January 22, 2015 02:17
Show Gist options
  • Select an option

  • Save walter/3e852e8ebc87535a3c91 to your computer and use it in GitHub Desktop.

Select an option

Save walter/3e852e8ebc87535a3c91 to your computer and use it in GitHub Desktop.
// app/initializers/options
import Ember from 'ember';
import config from '../config/environment';
// copied and modified from http://ember.zone/handling-environment-data-with-ember-js/
export default {
name: 'options',
initialize: function() {
var OptionsReader = function OptionsReader() {
this.readOptionsKeys = function() {
var _mapType = function(val) {
return "" === val ? null : "true" === val ? true : "false" === val ? false : (-1 !== val.indexOf(",") && (val = val.split(",")), val);
};
// Grab all the meta tags from the DOM.
var metaTags = Ember.$("meta");
var options = Ember.Object.create();
var re = new RegExp(config.modulePrefix + '\/');
// Process each of the discovered meta tags.
for(var i=0; i < metaTags.length; i++) {
var key = Ember.$(metaTags[i]).attr('name');
var value = Ember.$(metaTags[i]).attr('content');
// Does the meta tag start with our MODULE_PREFIX?
if (re.test(key)) {
// get the last element of path in name as property name
// all elements after that in name path are property name
// or parent elements that correspond to nested object
// create the parent object if they don't exist yet
// otherwise assign new property at proper nesting
var nameParts = Ember.A(key.split('/'));
var propertyName = nameParts.get('lastObject');
// ignore MODULE_PREFIX/initializers/options
// also skip actual propertyName
var parents = nameParts.filter(function(item, index) {
if (index > 2 && item !== propertyName) {
return true;
}
return false;
});
// loop through nesting and create parent objects if necessary
var currentObject = options;
parents.forEach(function(item, index) {
if (!currentObject.hasOwnProperty(item)) {
currentObject[item] = Ember.Object.create();
}
currentObject = currentObject[item];
// is this last parent, should we set property value?
if (parents.get('length') === index + 1) {
// Map the string values to actual types.
currentObject[propertyName] = _mapType(value);
}
});
}
}
return options;
};
};
var optionsReader = new OptionsReader();
// config.options.setProperties(optionsReader.readOptionsKeys());
config.options = optionsReader.readOptionsKeys();
}
};
// app/controllers/application.js
import Ember from 'ember';
import config from '../config/environment';
export default Ember.Controller.extend({
options: config.options
});
// app/templates/application.hbs
<h2 id="title">{{options.question.summary}}</h2>
{{outlet}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment