[英]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.