流星模板中的动态计算值
Dynamic calculated values in meteor templates
我正在尝试掌握一些我确信在 meteor 中非常基本的东西,即使用反应计算值。这是我试图实现的那种事情的简单示例。该代码不起作用,但它看起来像我期望的那样。我正在使用自动表单进行架构验证
Schema = {}
Schema.calcs = new SimpleSchema({
a: {type: Number},
b: {type: Number}
});
Template.Calcs.helpers({
total: function() {
var a = Autoform.getFieldValue('calcs', 'a')
var b = Autoform.getFieldValue('calcs', 'b');
// Would be nice if I could do something like this instead of the above
// var a = this.Autoform.a;
// var b = this.Autoform.b;
return a + b;
},
totalDouble: function() {
var total = this.total; // This doesn't work
return total * 2;
}
});
模板类似于:
<template name='calcs'>
{{> quickForm id="calcs" schema="Schema.calcs" validation="keyup"}}
<ul>
<li>Total: {{total}}</li>
<li>Double Total: {{totalDouble}}</li>
<ul>
</template>
我有 3 个问题:
- 如何在代码中获取另一个助手的值?
- 有没有比
Autoform.getFieldValue(...)
更简洁的方法来获取助手中的输入值?
- 这真的是用 Meteor 实现我想要的最好方法吗?
这实际上是我从 Ember 迁移的一个测试项目,我想要的行为是在 Ember 控制器(不包括验证)中实现的,如下所示:
App.CalcsController = Ember.Controller.extend({
a: null,
b: null,
total: function() {
return this.get('a') + this.get('b');
}.property('a', 'b'),
totalDouble: function() {
return this.get('total') * 2;
}.property('total')
});
您可以将其存储在会话中:
total: function() {
var a = parseInt(Autoform.getFieldValue('calcs', 'a'))
var b = parseInt(Autoform.getFieldValue('calcs', 'b'));
var total = a+b;
Session.set("total", total);
return total;
},
totalDouble: function(){
var total = Session.get("total");
return total* 2;
}
我正在尝试掌握一些我确信在 meteor 中非常基本的东西,即使用反应计算值。这是我试图实现的那种事情的简单示例。该代码不起作用,但它看起来像我期望的那样。我正在使用自动表单进行架构验证
Schema = {}
Schema.calcs = new SimpleSchema({
a: {type: Number},
b: {type: Number}
});
Template.Calcs.helpers({
total: function() {
var a = Autoform.getFieldValue('calcs', 'a')
var b = Autoform.getFieldValue('calcs', 'b');
// Would be nice if I could do something like this instead of the above
// var a = this.Autoform.a;
// var b = this.Autoform.b;
return a + b;
},
totalDouble: function() {
var total = this.total; // This doesn't work
return total * 2;
}
});
模板类似于:
<template name='calcs'>
{{> quickForm id="calcs" schema="Schema.calcs" validation="keyup"}}
<ul>
<li>Total: {{total}}</li>
<li>Double Total: {{totalDouble}}</li>
<ul>
</template>
我有 3 个问题:
- 如何在代码中获取另一个助手的值?
- 有没有比
Autoform.getFieldValue(...)
更简洁的方法来获取助手中的输入值? - 这真的是用 Meteor 实现我想要的最好方法吗?
这实际上是我从 Ember 迁移的一个测试项目,我想要的行为是在 Ember 控制器(不包括验证)中实现的,如下所示:
App.CalcsController = Ember.Controller.extend({
a: null,
b: null,
total: function() {
return this.get('a') + this.get('b');
}.property('a', 'b'),
totalDouble: function() {
return this.get('total') * 2;
}.property('total')
});
您可以将其存储在会话中:
total: function() {
var a = parseInt(Autoform.getFieldValue('calcs', 'a'))
var b = parseInt(Autoform.getFieldValue('calcs', 'b'));
var total = a+b;
Session.set("total", total);
return total;
},
totalDouble: function(){
var total = Session.get("total");
return total* 2;
}