Click here to Skip to main content
15,867,305 members
Articles / Programming Languages / Javascript
Tip/Trick

How To Populate a Select Element with Data From a JSON File in a Meteor App

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
3 Sep 2015CPOL1 min read 11.2K   1   2
Populating Select Elements' Options Values with "lookup" data stored in your settings.json file in your Meteor apps

The No-No SQL Way of populating Select Elements' Options Values

In a previous tip here, I showed how you can insert values into a MongoDB ("No SQL") Collection and then read them out using Meteor's Spacebars template language to populate a Select elements' Options values. This tip is similar but, instead of storing the data in and retrieving it out of a MongoDB Collection, it is stored/retrieved from a .json file. Here are the steps to do that:

First, put some data into a file, perhaps named settings.json, that resides directly beneath your project folder:

JavaScript
{"public" : { "uccampus": [ {"name":"UC Santa Cruz"}, {"name":"UC Berkeley"}, 
{"name":"UC Davis"}, {"name":"UC Irvine"}, {"name":"UC Los Angeles"}, 
{"name":"UC Merced"}, {"name":"UC Riverside"}, {"name":"UC San Diego"}, 
{"name":"UC San Francisco"}, {"name":"UC Santa Barbara"} ] }}
}

Second, add a helper for the Template in which the Select element resides, like so:

JavaScript
Template.tblTravelerInfo2.helpers({
    uccampus: function(){
        return Meteor.settings.public.uccampus;
    }
});

Third, add this to the HTML for that Select element (instead of things like "<option value="dp">Duckbilled Platypus</option>"):

JavaScript
{{#each uccampus}}
  <option>{{name}}</option>
{{/each}}

Note: When using this code, and running your Meteor app from its directory in the console, it is not enough to simply enter "meteor run" or "meteor". You need to enter "meteor --settings settings.json" (where "settings.json" is the name of the file containing the json data); and the said json file must reside directly beneath your project's folder - not in a subfolder.

Don't Neglect the Superhero's Superhero (After Meteor Man, of course)

If you like this tip, tip your hat to Top Cat the next time that you see him. If you have no hat, tip your toupée; if you don't wear a toupée, tou·ché Turtle soup of the day Trip the Light Fantastic Four Horsemen of the Apocalypse sinks ships ahoy, Matey s. Eliott Gould.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
GeneralMy vote of 5 Pin
PVX0078-Sep-15 5:39
PVX0078-Sep-15 5:39 
GeneralRe: My vote of 5 Pin
B. Clay Shannon8-Sep-15 5:46
professionalB. Clay Shannon8-Sep-15 5:46 
You're welcome; I find the best way for things to have a better chance of "sticking" in my thick noggin is to record them for posterity out as soon as I learn them myself.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.