【发布时间】:2017-01-09 08:04:13
【问题描述】:
我在数据上下文方面遇到问题;
这是我的代码(不幸的是,meteorpad 坏了)
router.js(我用的是iron:router)
Router.configure({
layoutTemplate: 'layout'
});
Router.route('home',{
path: '/',
action: function(){
this.redirect('sections', {page: 0});
}
});
Router.route('sections', {
path: '/sections/:page',
data: function(){
var data = {};
data.params = {};
data.params.page = this.params.page?this.params.page:0;
return data;
}
});
template.html
<template name="layout">
{{>yield}}
</template>
<template name="sections">
Page: {{params.page}}
<br>
<a href="{{pathFor 'sections' page=0}}">Page 0</a>
<a href="{{pathFor 'sections' page=1}}">Page 1</a>
<a href="{{pathFor 'sections' page=2}}">Page 2</a>
<br>
<button>what page?</button>
</template>
template.js
Template.sections.onRendered(function(){
let scope = this;
$("button").on("click", function(){
alert("page: " + scope.data.params.page);
});
});
当我单击按钮时,按钮处理程序具有范围,该范围在呈现时具有模板,但此时不实际;
【问题讨论】:
-
Blaze.getData(scope.view)代替scope.data有效吗?顺便说一句,您最好使用Template.sections.events({...})而不是使用 jQuery。 -
是的! Blaze.getData(scope.view) 有效!谢谢!如果可能,我会使用 Template.sections.events({})。
标签: node.js meteor meteor-blaze