
从模板助手调用 Meteor 方法
在 Meteor 中,模板助手提供了一种动态生成模板内容的方法。一个常见的要求是从这些助手中调用服务器端 Meteor 方法。
Meteor 0.9.3.1 为这种情况引入了一种新方法:
使用反应变量:
-
创建响应变量:
1 2 3 | <code class = "js" >Template.helloWorld.created = function () {
this.myAsyncValue = new ReactiveVar( "Waiting for response from server..." );
}</code>
|
登录后复制
-
调用方法:
1 2 3 4 5 6 7 | <code class = "js" >Meteor.call( 'getAsyncValue' , function (err, asyncValue) {
if (err) {
console.log(err);
} else {
this.myAsyncValue.set(asyncValue);
}
});</code>
|
登录后复制
-
使用帮助器:
1 2 3 4 5 | <code class = "js" >Template.helloWorld.helpers({
txt: function () {
return this.myAsyncValue.get();
}
});</code>
|
登录后复制
在此方法中,附加了响应式变量 this.myAsyncValue到模板实例。辅助函数返回此变量的值,该值会在方法回调触发时更新。
注意:此方法需要安装reactive-var 包:
1 | $ meteor add reactive- var
|
登录后复制
以上是如何使用反应变量从模板助手调用 Meteor 方法?的详细内容。更多信息请关注PHP中文网其他相关文章!