简体   繁体   English

在 ember 模板渲染后执行操作(javascript 函数)

[英]execute action (javascript function) after ember template is rendered

I have very small web page with emberjs, where I want to show some item list and openlayers map for them, and another map for selected item.我有一个非常小的带有 emberjs 的网页,我想在其中显示一些项目列表和 openlayers 地图,以及另一个选择项目的地图。 Something like that:类似的东西:

<script type="text/x-handlebars" id="list">
<div class="list">
    <div id="list_map"></div>
</div>
{{outlet}}
</script>

<script type="text/x-handlebars" id="list/item" >
<div class="item">
    <div id="item_map"></div>
</div>
</script>
<script>
function showListMap() {
    listMap = new ol.Map({target:'list_map'});
}
function showItemMap() {
    itemMap = new ol.Map({target:'item_map'});
}
</script>

There is no problem to display map for list:列表显示地图没有问题:

model: function(params) {
        var content = [];
        var url = 'http://localhost:8080/app/list'; 
        $.ajax({
          url: url,
          success: function(surveys) {            
            content.pushObjects(surveys);
            showListMap();
          }
        });
        return content;
}

and I have action in item controller that is executed, when opening selected item, but if I try to create item map there (in controllers action) it fails (afaik because there is no such div at that moment in DOM).并且我在打开所选项目时在项目控制器中执行了操作,但是如果我尝试在那里创建项目映射(在控制器操作中),它会失败(afaik,因为当时在 DOM 中没有这样的 div)。 So if I could execute action or my function after div is already add to DOM, it should work.因此,如果我可以在 div 添加到 DOM 之后执行操作或我的函数,它应该可以工作。 And question would be, how to execute something after template is added to DOM, or that's completely wrong way to do such stuff (than what would be correct ember way)?问题是,如何在将模板添加到 DOM 后执行某些操作,或者这是完全错误的方法(而不是正确的 ember 方法)?

I can't say much with seeing full code.看到完整的代码,我不能说太多。 But to execute some code after the DOM is rendered you schedule a function on the the run loops afterRender queue.但是要在渲染 DOM 后执行一些代码,您可以在运行循环afterRender队列上安排一个函数。

Ember.run.scheduleOnce('afterRender', this, function() {
  //The div should be available now.
});

But if you really need to touch the DOM I recommend you wrap your map code in a component.但是如果你真的需要接触 DOM,我建议你将地图代码包装在一个组件中。 A component gets a didInsertElement where you can write your maps initialization code.组件获得一个didInsertElement ,您可以在其中编写地图初始化代码。

var component = Em.Component.extend({
  setup: function() {
    //Do you map init here.
  }.on('didInsertElement')
});

There unfortunately isn't a really good route or controller hook that fires off after a page has already rendered.不幸的是,没有一个真正好的路由或控制器钩子在页面已经呈现后触发。 I believe the reason for this is that the developers of Ember think it is an anti-pattern to directly talk to the DOM.我相信这是因为 Ember 的开发人员认为直接与 DOM 对话是一种反模式。

That being said, I think it sometimes is quite handy for complex UI on otherwise static web pages.话虽如此,我认为它有时对于静态网页上的复杂 UI 非常方便。 If you want to do some sort of jquery or use the DOM API after a route has rendered, you can do the following in your route file (as @Dainius correctly points out)如果您想在路由呈现后执行某种 jquery 或使用 DOM API,您可以在路由文件中执行以下操作(正如@Dainius 正确指出的那样)

routeName.js路由名称.js

import Route from '@ember/routing/route'; 
import jQuery from 'jquery';

export default class myRouteFile extends Route {

  manipulateDom = function() {
     $("#myDiv").css( "color", "red" );
  }

  init() {
    this._super(...arguments)
    Ember.run.scheduleOnce('afterRender', this, this.manipulateDom)
  }
}

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

相关问题 渲染AngularJS模板后执行Javascript - Execute Javascript after AngularJS template rendered 余烬模板中的循环完成后执行jQuery函数 - Execute jQuery function after loop in ember template finishes 在呈现主模板后执行子模板中存在的 javascript 代码 - Execute javascript code present inside child template after the main template is rendered GWT UiBinder-渲染模板后执行JS吗? - GWT UiBinder - Execute JS after template is rendered? 如何使用JavaScript在@ Url.Action之后执行其他功能 - How to execute other function after @Url.Action with javascript 在Struts操作执行一些计算后执行javascript函数 - Execute javascript function after Struts action do some calcul 离子框架,在呈现模板后加载javaScript - ionic framework, load a javaScript after template is rendered 为什么模板没有在此Ember应用程序中呈现? - Why is the template not rendered in this Ember Application? 是否可以将函数传递给Ember.js组件以将其作为动作回调执行? - Is it possible to pass a function to an Ember.js component to execute it as an action callback? 自升级到Ember2.4以来,呈现的子模板中的操作未冒泡路由 - Action in rendered sub template not bubbled to route since upgrade to Ember2.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM