我正在尝试使用Meteor创建自定义帮助程序.关于这里的文档:https: //github.com/meteor/meteor/wiki/Handlebars
我试图按如下方式定义我的助手:
Template.myTemplate.testHelper = function(foo, bar, options) {
console.log(foo);
console.log(bar);
}
Run Code Online (Sandbox Code Playgroud)
我的模板看起来像:
<template name="myTemplate">
{{#testHelper "value1" "value2"}}
{{/testHelper}}
</template>
Run Code Online (Sandbox Code Playgroud)
看看我的控制台输出,我希望看到2行输出:
value1
value2
Run Code Online (Sandbox Code Playgroud)
但是我的控制台看起来像:
value1
function (data) {
// don't create spurious annotations when data is same
// as before (or when transitioning between e.g. `window` and
// `undefined`)
if ((data || Handlebars._defaultThis) ===
(old_data || Handlebars._defaultThis))
return fn(data);
else
return Spark.setDataContext(data, fn(data));
}
Run Code Online (Sandbox Code Playgroud)
请注意,我对流星和车把都是全新的.我想使用下划线会更开心,但是流星的文档几乎完全瞥了一眼下划线.我在定义辅助函数时做错了吗?它似乎没有看到第二个参数"bar",而是将其解释为选项.(注意:如果我是console.log(options),则返回'undefined').
流星版本0.4.0(8f4045c1b9)
如何从另一个模板助手中引用模板助手?例如...
Template.XXX.helpers({
reusableHelper: function() {
return this.field1 * 25 / 100; //or some other result
},
anotherHelper: function() {
if (this.reusableHelper() > 300) //this does not work
return this.reusableHelper() + ' is greater than 300';
else
return this.reusableHelper() + ' is smaller than 300';
}
});
Run Code Online (Sandbox Code Playgroud)
我也尝试过Template.instance().__ helpers.reusableHelper - 一切都没有运气.
或者有没有办法定义反应模板实例变量?
XXX是一个在同一页面上呈现多次的子模板.
我正在使用Meteor的帐户-ui.有没有办法检查用户是否登录模板而不编写自定义帮助程序代码?
伪代码:
{{#if userIsLoggedIn }}
You're logged in
{{/if}}
Run Code Online (Sandbox Code Playgroud)
如果没有,那么最干净,最惯用的方式是什么?
我在这里只关心客户端.
谢谢.
如何使用each输入值events?希望我的下面代码能很好地解释你.
HTML:
<template name="UpdateAge">
{{#each Name}}
<div data-action="showPrompt">
<div>
Some content
</div>
<div>
<div>
Some content
</div>
</div>
<div>
Name : {{name}}
Age : <input type="text" name="age" value="{{age}}"/> //I need to access age values in event
</div>
</div>
{{/each}}
<template>
Run Code Online (Sandbox Code Playgroud)
JS:
Template.UpdateAge.events({
'click [data-action="showPrompt"]': function (event, template) {
console.log(event.target.age.value); // TypeError: event.target.age.value is undefined
}
});
Run Code Online (Sandbox Code Playgroud)
我不知道我的方法是否适合传递参数值,events所以欢迎提出建议.
我正在尝试如何获取Image(使用CollectionFS的文件)并将Image的Id插入到我的Items imageId字段中:
LIB /收藏/ items.js
Items = new Mongo.Collection("items");
Items.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Name",
},
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoform: {
type: "hidden",
label: false
},
autoValue: function () { return Meteor.userId() },
},
image: {
type: String,
optional: true,
autoform: {
label: false,
afFieldInput: {
type: "fileUpload",
collection: "Images",
label: 'Select Photo',
}
}
},
imageId: {
type: String
}
}));
Run Code Online (Sandbox Code Playgroud)
LIB /收藏/ images.js
if (Meteor.isServer) {
var imageStore = new …Run Code Online (Sandbox Code Playgroud) 如何为流星中的所有模板创建一个函数?
index.js
// Some function
function somefunction(){
return true;
}
Run Code Online (Sandbox Code Playgroud)
Test1.js
Template.Test1.events({
'click button' : function (event, template){
//call somefunction
}
});
Run Code Online (Sandbox Code Playgroud)
Test2.js
Template.Test2.events({
'click button' : function (event, template){
//call some function
}
});
Run Code Online (Sandbox Code Playgroud) 在许多模板中,我想使用相同的功能,但必须在每个模板中定义它们.像这样:
function getNodesById(id){
return collection.find({sid:id}).fetch();
}
Template.navigation.getNodesById= function(id){
return getNodesById(id);
}
Template.body.getNodesById= function(id){
return getNodesById(id);
}
Run Code Online (Sandbox Code Playgroud)
HTML:
Run Code Online (Sandbox Code Playgroud)<Template name="navigation"> ... {{#each getNodesById '1'}} ... {{/each}} ... </Template> <Template name="body"> ... {{#each getNodesById '1'}} ... {{/each}} ... </Template> ... <Template name="..."> ..... </Template>
有没有办法可以定义全局模板功能而不是模板?就像它:在javascript中:
defined global tempele.functionA = function(...){
return ...
}
在HTML中:
Run Code Online (Sandbox Code Playgroud)<Template name ="a"> {{#each functionA ...}} {{/each }} </Template> <Template name ="b"> {{#each functionA ...}} {{/each }} </Template> <Template name="..."> {{ #.. functionA ...}} .... {{/...}} </Template > …
假设我有这个Handlebars助手:
Handlebars.registerHelper('someRandomHelperCreatingALink', function(passedVarAndString, url) {
return '<a href="'+url+'">'+passedVarAndString+'</a>';
});
Run Code Online (Sandbox Code Playgroud)
并希望像这样使用它,我传递一个字符串和一个var作为第一个参数(user.name+' is a cool dude!'):
{{{ someRandomHelperCreatingALink user.name+' is a cool dude!!' '/a/cool/url' }}}
Run Code Online (Sandbox Code Playgroud)
我的问题:这会以某种方式成为可能吗?
或者我必须为字符串添加额外的参数(这会觉得没必要)?像这样的东西:
Handlebars.registerHelper('someRandomHelperCreatingALink', function(passedVarAndString, url, extraUnnecessary) {
return '<a href="'+url+'">'+passedVarAndString+extraUnnecessary+'</a>';
});
{{{ someRandomHelperCreatingALink user.name '/a/cool/url' ' is a cool dude!!' }}}
Run Code Online (Sandbox Code Playgroud) 所以,假设我正在存储<div>{{name}}</div>并存储<div>{{age}}</div>在我的数据库中.然后我想获取第一个HTML字符串并在模板中呈现它 - {{> template1}}它只是呈现带有{{name}}把手的第一个字符串.然后我想给出新生成的模板/ html数据,这样它就可以用name数据库中的实际来填充把手,这样我们就可以得到<div>John</div>.我试过了
<template name="firstTemplate">
{{#with dataGetter}}
{{> template1}}
{{/with}}
</template>
Run Code Online (Sandbox Code Playgroud)
其中template1定义为
<template name="template1">
{{{templateInfo}}}
</template>
Run Code Online (Sandbox Code Playgroud)
而templateInfo是一个帮助器,它从数据库中返回带有把手的上述html字符串.
dataGetter就是这个(只是一个例子,我正在使用不同命名的集合)
Template.firstTemplate.dataGetter = function() {
return Users.findOne({_id: Session.get("userID")});
}
Run Code Online (Sandbox Code Playgroud)
我无法填充{{name}}.我尝试了几种不同的方法,但似乎Meteor不明白字符串中的把手需要用数据进行评估.我在0.7.0所以没有Blaze,由于我正在使用的其他软件包,我目前无法升级,他们还没有支持0.8+版本.关于如何让它工作的任何想法都非常感谢.
如何将值传递给模板事件
HTML
<template name="Header">
<div class="testClass">Text1</div> // pass a = 1
<div class="testClass">Text2</div> // pass a = 2
</template>
Run Code Online (Sandbox Code Playgroud)
使用Javascript
Template.Header.events({
'click .testClass':function(event, template){
console.log(a) //print a values
}
});
Run Code Online (Sandbox Code Playgroud) meteor ×10
meteor-helper ×10
javascript ×6
meteor-blaze ×6
spacebars ×3
templates ×2
collectionfs ×1