session - Meteor 的 Session.set 导致重复 Bootstrap 模式?

标签 session twitter-bootstrap duplicates modal-dialog meteor

我正在使用 Meteor 0.6.2.1。

我刚刚在调用 Meteor 的 Session.set() 时遇到了 Bootstrap 模式的一个奇怪问题。

我想显示一个简单的模式对话框,并在用户单击模板实例时更新一些数据。

我将 Bootstrap 模式示例复制到我的 .html 文件中:

    <body>
        {{> hello}}
        {{> alert}}
    </body>

    <template name="hello">
        <h1>Hello World!</h1>
        {{greeting}}
        <input type="button" value="Click" />
        <br/>

        <!-- Button to trigger modal -->
        <a href="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>
    </template>

    <template name="alert">
        <!-- Modal -->
        <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel">Modal header</h3>
            </div>
            <div class="modal-body">
                <p>One fine body…</p>
                <p>data = {{data}}</p>
            </div>
            <div class="modal-footer">
                <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
                <button class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </template>

并设置按钮点击时的数据:

if (Meteor.isClient) {
  Session.set("data", "abcd");
  Template.hello.greeting = function() {
    return "Welcome to use-bootstrap.";
  };
  Template.alert.data = function() {
    return Session.get("data");
  };
  Template.hello.events({
    'click input': function() {
      if (typeof console !== "undefined" && console !== null) {
        return console.log("You pressed the button");
      }
    }
  });
  Template.hello.events({
    'click .btn': function() {
      var randomId;
      randomId = Random.id();
      console.log("data = " + Session.get("data"));

      // this cause duplicate Template.alert
      Session.set("data", randomId);
    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function() {
    return console.log("Server Start!!");
  });
}

我使用chrome对其进行调试,发现单击按钮时模态元素将重复。

我的代码出了什么问题?

最佳答案

我不是 100% 确定为什么会发生这种情况,但我相信这与 JS 代码( Bootstrap )中保存的模态节点的引用有关。

为了解决这个问题,我添加了:

Template.alert.preserve(["#myModal"]);

来自 meteor docs :

Preservation is useful in a variety of cases where replacing a DOM element with an identical or modified element would not have the same effect as retaining the original element. These include:

  • Input text fields and other form controls
  • Elements with CSS animations
  • Iframes
  • Nodes with references kept in JavaScript code

关于session - Meteor 的 Session.set 导致重复 Bootstrap 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16373442/

相关文章:

node.js - Express.JS : Logout without destroying the session

cocoa - 登录页面的 session 维护

c# - 是否可以在 IE 和 Word 附加组件之间共享 ASP.NET session cookie

C++ 比较来自 2 个不同列表的值以使用 2 个 for 循环消除重复数字。无法正确检测重复项

javascript - 如何使用 ajax 请求 javascript/PHP 在远程服务器上存储 session

JQuery masonry.js 与 Json 数据 .append( ) 的问题

html - Bootstrap 响应式嵌入视频占据了整个屏幕的宽度和高度

html - 如何制作 Bootstrap 下拉菜单的垂直滚动条

sql - 计算重复项时自加入 vs 分组

python - 根据条件删除字典列表中的重复项