【问题标题】:Template actual data context模板实际数据上下文
【发布时间】: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


【解决方案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);
    });
});

【讨论】:

    猜你喜欢
    • 2016-05-02
    • 2019-04-08
    • 1970-01-01
    • 2014-12-30
    • 2018-01-03
    • 2017-05-20
    • 2014-02-25
    • 2017-08-24
    • 2018-08-26
    相关资源
    最近更新 更多