简体   繁体   English

在emberjs中渲染模板后如何使事情发生

[英]How to make something happen after a template has rendered in emberjs

So the concept is simple: You come to the app, you have a default template, with a default navigation element. 因此概念很简单: 您来到应用程序时,拥有一个默认模板和一个默认导航元素。 You click a link in that navigation element it renders a new template: #/rails . 您单击该导航元素中的链接,它将呈现一个新模板: #/rails

From here the default navigation needs to be hidden, and your new nav needs to be rendered. 从这里需要隐藏默认导航,并且需要渲染新的导航。

The way i attempted to approach this, seems a bit silly: what I did was, 我尝试解决此问题的方法似乎有点愚蠢:我所做的是,

SG.Router.map(function(){
  this.resource('rails');
});

$(document).ready(function() {
  if($('#rails-nav').length !== 0){
    $('#main-nav').hide();
  }
});

Now the issue with this is that if you go from the default application template, to the rails template via the link - you get two navs unless you refresh that page. 现在的问题是,如果您从默认应用程序模板通过链接转到Rails模板-除非刷新该页面,否则您将获得两个导航。 My friend stated that I should use something like: 我的朋友说我应该使用类似:

{{outlet nav}} and then render a navigation based on template. {{outlet nav}} ,然后根据模板呈现导航。 The issue is I don't know how to set this up and I have been looking all over the ember site. 问题是我不知道如何设置它,而且我一直在寻找余烬站点。

Could some one help me out? 有人可以帮我吗?

If i understood correctly, when you say default template you mean the application template, where all other templates are rendered within its {{outlet}} helper. 如果我理解正确,当您说default template ,是指application模板,其中所有其他模板都在其{{outlet}}帮助器中呈现。

There are several approaches to achieve what you want, but a simple one i think would be to use the index template as your default template. 有几种方法可以实现您想要的,但是我认为一个简单的方法就是将index模板用作您的默认模板。 In this case evertyhting will be much simpler and work as you require, since you can specify whether the navigation element is shown by placing it inside a template or not. 在这种情况下,套路将更加简单并且可以按需工作,因为您可以通过将导航元素放置在模板中来指定是否显示导航元素。

http://emberjs.jsbin.com/sume/1/edit http://emberjs.jsbin.com/sume/1/edit

hbs 血红蛋白

<script type="text/x-handlebars">
    <h2> Welcome to Ember.js</h2>

    {{outlet}}
  </script>

  <script type="text/x-handlebars" data-template-name="index">
    This is the default template using <i>index</i>
    <br/>
    <br/>
    <span style="background-color:grey">this is the nav part {{#link-to 'rails'}}go to rails{{/link-to}}</span>
  </script>

  <script type="text/x-handlebars" data-template-name="rails">
    this is the rails app
  </script>

js js

App = Ember.Application.create();

App.Router.map(function() {
  this.resource("rails");
});

Also to make this answer a bit more relevant to the title of the question, in order to do something when a template has rendered one way would be to use the didInsertElement callback of the View class. 同样,为了使此答案与问题的标题更加相关,为了在模板呈现一种方式后执行某项操作,将使用View类的didInsertElement回调。

App.RailsView = Ember.View.extend({
  didInsertElement:function(){
    if($('#rails-nav').length !== 0){
      $('#main-nav').hide();
    }
  }
});

http://emberjs.jsbin.com/dido/1/edit http://emberjs.jsbin.com/dido/1/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM