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.
我正在开发一个流星应用程序,该应用程序应该满足一个datalist,它将一些数据存储在JSON文件中,作为流程的一部分。但是,我没有成功地将它的数据导入到模板中。所以我想知道如何解决这个问题。
I have stored the JSON file into panel/skills.json, and it looks like this basically:
我已经将JSON文件存储到面板/技能中。json,它基本上是这样的:
{"skills":[
{"value":".NET Compact Framework"},
{"value":".NET Framework"},
{"value":".NET para Web"}
]}
And this is how the HTML file looks like:
这就是HTML文件的样子:
<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 :)
是否有办法将JSON文件放入.js归档文件中,并使用助手加载它?谢谢你:)
2 个解决方案
#1
1
I didnt try but this will work.
我没试过,但这行得通。
- load json file using require
- 使用require加载json文件
- store values to reactiveVar
- 存储值reactiveVar
- 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();
}
});
#2
0
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.
我还没有测试,但是我已经使用了类似的东西。
#1
1
I didnt try but this will work.
我没试过,但这行得通。
- load json file using require
- 使用require加载json文件
- store values to reactiveVar
- 存储值reactiveVar
- 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();
}
});
#2
0
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.
我还没有测试,但是我已经使用了类似的东西。