【问题标题】:Passing variable into a modal through spacebars通过空格键将变量传递给模态
【发布时间】:2017-07-18 23:19:01
【问题描述】:

有没有办法不用JavaScript,空格键可以将变量传递给模态框?

例如,当我尝试时:

<button type="button" id="genbtn" data-toggle="modal" data-target="#entryModal">Pass</button>
{{> newentry var=item}}

它在模态中不起作用:

<template name="newentry">
 {{#with var}}It displays here!{{/with}}
<div id="entryModal" class="modal fade" role="dialog">
 {{#with var}}Does not display here!{{/with}}

(模态的其余部分工作正常)

我不明白为什么变量可以进入模板,但不能通过模态。否则,模态的非 var 相关函数工作得很好。为什么会中断?能克服吗?如果有,怎么做?

注意:如果不使用 JavaScript 就无法工作,我可以做到,但感觉这个问题的解决方案是错误的。

【问题讨论】:

  • 您确定该项目已定义吗?您可以尝试其他名称而不是 var 吗?它可能是一个保留字。顺便说一句,如果您只需要显示一些文本,为什么不使用 {{#if}}?
  • 我没有使用'var',这是一个例子。我传递给模板的变量在模态类中工作得很好。至于使用#if 与使用#when,我得到完全相同的行为,即在 div 外部但不在内部工作。

标签: meteor spacebars


【解决方案1】:

我认为使用 Jquery lightbox_me 插件将您的模态包装到流星模板中会更好更简单。 例如。

$body = $('body')
UI.render Template.release_info, $body[0] //rendering template
$popup =  $body.find("#releases-info-popup-container") // modal Id
$popup.lightbox_me
  centered: true
  destroyOnClose: true

模板

<Template name="release_info">
   <div id="releases-info-popup-container">
   </div>
</Template>

【讨论】:

    【解决方案2】:

    我最近遇到了更棘手的问题,所以我只分享我的工作示例。我正在使用下面的代码,它工作得很好。

    {{>userInfo userId=customer._id userType='Customer'}}
    

    然后在 userInfo 模板中,我将 userType 设置为:

    <div class="panel panel-{{currentColor ../userType}} user-info">
    

    请注意 userType 之前的../。我也在js中使用userId:

    Template.currentData().userId
    

    所以可以以任何方式使用,就像我作为助手一样

    relatedUser: function(){
            return Meteor.users.findOne({_id: Template.currentData().userId})
    }
    

    希望我的例子能有所帮助:)

    更新

    在 newentry 模板中创建一个助手:

    myReactiveHelper: function(){
        return Template.currentData().var
    }
    

    然后在您的模板中将该帮助程序而不是 var 绑定为:

    {{#with myReactiveHelper}}Does not display here!{{/with}}
    

    【讨论】:

    • 似乎没有为我做任何事情。看起来我现在将不得不使用 JavaScript 来解决我的问题。只是不知道为什么模态类会破坏我的数据上下文!
    • @CodeDragonDM 将var 更改为任何其他名称并考虑嵌套模型层。如果您在子模型中有模型,您可以使用 ../../varname
    • 'var' 不是我使用的名称,只是一个示例。再次使用正确的变量名称,它将数据获取到模板,在进入模态“div”之前就死了。使用 '../varname' 或 '../../varname' 也不起作用。
    猜你喜欢
    • 1970-01-01
    • 2022-12-24
    • 2021-03-02
    • 2023-03-18
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多