我遇到数据上下文的麻烦;
这是我的代码(不幸的是,meteorpad坏了)
router.js(我用铁:路由器)
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);
});
});
当我点击按钮时,按钮处理程序具有范围,该范围具有模板,在渲染时,但在此时不实际;
答案 0 :(得分:1)
感谢@ user3374348
方法Blaze.getData(scope.view)返回实际的数据上下文。
<强> template.js 强>
Template.sections.onRendered(function(){
let scope = this;
$("button").on("click", function(){
alert("page: " + Blaze.getData(scope.view).params.page);
});
});