假设我有一个对象,someObject:
{
foo: "apple",
myArray: ["abc", "def"]
}
Run Code Online (Sandbox Code Playgroud)
还有一个看起来像这样的模板助手(并且工作正常):
getArray: function(){
var self = this;
self.myArray = self.myArray || [];
return self.myArray;
}
Run Code Online (Sandbox Code Playgroud)
我应该如何构造html来获取数组索引?
我试过了:
<template name="someObject"> // takes someObject as data
{{#each getArray}}
<div class="item" data-value="{{WHAT GOES HERE?}}">{{this}}</div>
{{/each}}
</template>
Run Code Online (Sandbox Code Playgroud)
在哪种情况下this成功返回"abc"和"def".这很好.但是如何才能将数组的索引放入属性中data-value?
我已经this.index直接尝试但它未定义.我也尝试过使用帮手:
<template name="someObject"> // takes someObject as data
{{#each getArray}}
<div class="item" data-value="{{getindex}}">{{this}}</div>
{{/each}}
</template>
Run Code Online (Sandbox Code Playgroud)
但是getIndex当我在console.log中时,在这个帮手中,this我看到:
String {0: "a", 1: "b", 2: "c", length: …Run Code Online (Sandbox Code Playgroud) 如何从另一个模板助手中引用模板助手?例如...
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是一个在同一页面上呈现多次的子模板.
我正在努力做我认为应该是一项非常简单的任务,但在过去一小时内没有这样做.如果用户属性与值匹配,我想默认选择一个选择选项.
<select name="myName">
{{#each addKeys myTable}} <!-- addKeys creates variables for keys and values -->
<option value="{{key}}" {{#if currentUser.property === key}}selected="selected"{{/if}}>{{value}}</option>
{{/each}}
</select>
Run Code Online (Sandbox Code Playgroud)
现在我觉得这很容易实现.但事实证明,Spacebars不允许除否定感叹号之外的条件运算符,因此等号无关紧要.然后我尝试了一些可怕的尝试:
在模板中myTemplate:
<select name="myName">
{{#each addKeys myTable}}
<option value="{{key}}" {{isSelected currentUser.property key}}>{{value}}</option>
{{/each}}
</select>
Run Code Online (Sandbox Code Playgroud)
在mytemplate.js:
Template.myTemplate.helpers({
isSelected: function(v1, v2) {
if (v1 === v2)
return "selected=\"selected\"";
return '';
}
});
Run Code Online (Sandbox Code Playgroud)
这段代码不仅可怕,看起来很糟糕,它不起作用:
Exception in Meteor UI: String contains an invalid character
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这么简单的事情似乎无法实现.我错过了什么吗?
我希望能够使用流星模板助手动态指定content一个的<meta>标签.似乎没有办法做到这一点.
如果我将<meta>标记放在自由浮动<head>元素中(即不在模板中),则两者都将正确包含在HTML中,但我不能使用模板助手.
如果我移动<meta>到模板,并尝试在自由浮动<head>元素内渲染模板,它会抱怨.
如果我将整个<head>元素移动到模板中,现在我有一个<head>嵌套在<body>其中的块,这很难看,我怀疑HTML无效(尽管Chrome似乎优雅地处理它).
有解决方案吗?
我在此数据库中有一个包含空格键的列,我想要更改.
ALTER TABLE . CHANGE COLUMN `Anzahl Personen` AnzahlPersonen int(11);
Run Code Online (Sandbox Code Playgroud)
在命令行中使用此行后,输出如下:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CHANGE COLUMN `Anzahl Personen` AnzahlPersonen int(11)' at line 1
Run Code Online (Sandbox Code Playgroud)
是的,我不知道,我做错了什么.
我想访问流星模板中的数组的第一项,我正在使用这种语法:
<p>{{array[0]}}</p>
Run Code Online (Sandbox Code Playgroud)
然而,它似乎并没有起作用.我不想使用值迭代{{#each}},只需从数组中选择第一个.
在许多模板中,我想使用相同的功能,但必须在每个模板中定义它们.像这样:
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 > …
我正在使用{{#each}}在Meteor中迭代一个集合,我想知道我是否在最后一个元素中,就像我在AngularJS中使用ngRepeat和$ last一样.
例如,它可用于构建人类可读的枚举,如"我喜欢猫,狗和海豚":
Template.myTemplate.helpers({
likedAnimals: function(){return ['dogs','cats','dolphins'];}
});
<template name='myTemplate'>
I like
{{#each likedAnimals}}
{{#if !$first && !$last}}, {{/if}}
{{#if $last}} and {{/if}}
{{this}}
{{/each}}
</template>
Run Code Online (Sandbox Code Playgroud)
有没有办法检查Meteor中的这种情况?
我想if在Meteor Blaze模板中使用一个条件.假设您users在要复制任务的Users集合上有一个帮助,如果用户名是admin,则使用"红色"样式:
<ul>
{{#each users}}
<li {{#if(name==admin)}}class="red"{{/if}}>{{name}}</li>
{{/each}}
</ul>
Run Code Online (Sandbox Code Playgroud) 我还没有找到一个可靠的例子.
Template.registerHelper("itemLookup", function(sku, property){
return Items.findOne({sku: sku})[property];
});
Run Code Online (Sandbox Code Playgroud)
我如何在模板上调用它?
我想做的事情如下:
{{ itemLookup sku="i3_4030U" property="title" }}
Run Code Online (Sandbox Code Playgroud)
它应该输出
"Intel Core i3 4030U"
Run Code Online (Sandbox Code Playgroud) spacebars ×10
meteor ×9
javascript ×5
meteor-blaze ×2
equals ×1
html ×1
if-statement ×1
mariadb ×1
mongodb ×1
node.js ×1