Caike Motta
Caike Motta

Reputation: 193

Load JSON content into a meteor template

I'm working on a Meteor application that should fulfil a datalist with some data stored in a JSON file as part of a process. However, I didn't succeed in importing its data to a template. So I was wondering how I could solve this.

I have stored the JSON file into panel/skills.json, and it looks like this basically:

{"skills":[
{"value":".NET Compact Framework"},
{"value":".NET Framework"},
{"value":".NET para Web"}
]}

And this is how the HTML file looks like:

<div class="required">
    <input type="text" class="form-control" list="tags">
    <datalist class="form-control" id="tags" name="tags">
    {{#each skills}}
        <option value={{value}}></option>
    {{/each}}
    </datalist>
</div>

Is there any way of getting the JSON file into a .js archive, and load it using helpers? Thank you :)

Upvotes: 2

Views: 495

Answers (2)

Ramesh Murugesan
Ramesh Murugesan

Reputation: 5013

I didnt try but this will work.

  1. load json file using require
  2. store values to reactiveVar
  3. return from helper

Example:

var json = require('panel/skills.json');

Template.myTemplate.onCreated(function() {
 this.skills = new ReactiveVar(json);
});

Template.myTemplate.helpers({
    skills: function (){
        return Template.instance().skills.get();
    }
});

Upvotes: 1

Use Can create a method in you Meteor.methods

meteor.methods({
    getSkills: function (){
        var Skills = JSON.parse(Assets.getText("parse/skills.json"));
        return Skills.skills;
    }
})

And now invoke this method in your template

Template.skills.helpers({
    skills: function (){
        Meteor.call('getSkills', function(err, result){
           return result;
        }
    }
})

I did not test yet, but I alread use something like these.

Upvotes: 0

Related Questions