【问题标题】:Calling a jquery event after the page transitions页面转换后调用 jquery 事件
【发布时间】:2014-10-14 18:48:35
【问题描述】:

例如,从/application 转换到/application/faq$('.object').show(); 在/application 加载时被触发,但是当我转换到application/faq 时它不会再次触发!

也许这就是didInsertElement 的工作原理,但我需要在每次转换到具有类`object 的dom 元素的任何地方后都会调用的东西。

对于这个例子,类“对象”是我想要切换的。

'application_view' on didInsertElement {
   $('.object').show();
}

-'application.hbs' HTML snippet-
<div class="object"></div>

-'faq.hbs' HTML snippet-
<div class="object"></div>

all css
  .object {
   display:none
  }

这是棘手的部分:我希望在 application.hbsfaq.hbs 中都从 application_view 激活 $('.object').show();

【问题讨论】:

    标签: javascript jquery html ember.js


    【解决方案1】:

    这是一个使用 jQuery 在发生不同转换时显示/隐藏警报的示例。单击“运行代码 sn-p”以查看示例。单击 ABC 链接以转换到这些路线并查看 jquery 动画。

    var App = Ember.Application.create();
    
    App.Router.map(function() {
      this.resource('a');
      this.resource('b');
      this.resource('c');
    });
    
    App.ApplicationRoute = Ember.Route.extend({
      actions: {
        willTransition: function() {
          Ember.$('.alert').fadeOut(200);
        },
        didTransition: function() {
          Ember.$('.alert').fadeIn(200);
        }
      }
    });
    .active {
      border: 2px #888 solid !important;
      padding: 2px 2px 3px 2px;
    }
    #main {
      margin: 1em;
    }
    .alert {
      margin-top: 1em;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://builds.emberjs.com/handlebars-1.0.0.js"></script>
    <script src="http://builds.emberjs.com/ember-latest.js"></script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    
    <script type="text/x-handlebars" id="application">
      <div id="main">
        {{#link-to 'a'}}<span class="label label-default">A</span>{{/link-to}} {{#link-to 'b'}}<span class="label label-default">B</span>{{/link-to}} {{#link-to 'c'}}<span class="label label-default">C</span>{{/link-to}} {{outlet}}
        <div class="alert alert-success">Success!</div>
      </div>
    </script>

    【讨论】:

    • 谢谢史蒂夫!使用 did 和 willTransition 时,我能够在加载路由之前和之后显示 2 个控制台日志。但是警报不会出现。同样,当我尝试使用 $().addClass() 向 dom 元素添加类时,没有添加类
    • 这些听起来像是与您的问题无关的问题。我建议编辑您的问题并添加更多代码以获得更多帮助。
    • 尝试使 jquery 事件激活后转换与我的问题“在页面转换后调用 jquery 事件”有何不同?
    • 我的假设是你的问题是关于执行与 Ember 转换相关的代码的正确钩子。您在评论中描述的问题(而不是您的问题)是关于 jQuery 的使用,而不是代码运行时。您没有在问题中提到这一点,也没有发布代码,假设您将其更改为使用 willTransition / didTransition 技术。
    • 实际上这是我的问题,因为它是标题。其次,我在评论中描述的问题与 didTransition 函数没有响应我的 jquery (标题中再次提到)有关。它可以是普通的 js 或任何操纵 dom 的东西,这不是重点。关键是 didTransition 函数在转换后没有调用 jquery 甚至你的警报。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2014-09-20
    • 1970-01-01
    相关资源
    最近更新 更多