使用 HTMLImports.whenReady 在 chrome 中不起作用 [英] using HTMLImports.whenReady not working in chrome

查看:53
本文介绍了使用 HTMLImports.whenReady 在 chrome 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 HTMLImports.whenReady 的 web 组件来确保某些东西在正确的时间触发,问题是这修复了我在 IE 和 Firefox 中遇到的错误,但现在它在 chrome 中不起作用.

所以在网站的页面上我有这个:

<link rel="import" href="/static/template/components/result-options.html"><link rel="import" href="/static/template/components/advanced-modal.html"><link rel="import" href="/static/template/components/map-modal.html"><div class="row-fluid"><div id="component" class="span12"><div class="well"><!-- TMPL_IF 列表--><div class="row-fluid"><small><em>按地图标记获取更多信息</em></small><br/><结果图></结果图>

<!--/TMPL_IF -->

<脚本>/*生成地图标记*/HTMLImports.whenReady(function(){<!-- TMPL_IF 列表--><!-- TMPL_LOOP 列表--><!-- TMPL_IF have_geocode -->标记.推({latLng: [<!-- TMPL_VAR 纬度-->, <!-- TMPL_VAR 经度-->],数据: '...',选项: {icon: "/static/images/mapmarker.png",title: "<!-- TMPL_VAR street_no --> <!-- TMPL_VAR 街道-->, <!-- TMPL_VAR 城市-->, <!-- TMPL_VAR 州--> <;!-- TMPL_VAR zip -->"}});<!-- TMPL_ELSE -->标记.推({地址:"<!-- TMPL_VAR street_no --> <!-- TMPL_VAR 街道-->, <!-- TMPL_VAR 城市-->, <!-- TMPL_VAR 州--> <;!-- TMPL_VAR zip -->",数据: '...',选项: {icon: "/static/images/mapmarker.png",title: "<!-- TMPL_VAR street_no --> <!-- TMPL_VAR 街道-->, <!-- TMPL_VAR 城市-->, <!-- TMPL_VAR 州--> <;!-- TMPL_VAR zip -->"}});<!--/TMPL_IF --><!--/TMPL_LOOP --><!--/TMPL_IF -->});

然后聚合物 webcomponent 看起来像这样

<模板><div id="map_canvas"></div><脚本>var 标记 = [];HTMLImports.whenReady(function(){$('#map_canvas').gmap3({地图:{选项:{街景控制:假,mapTypeId: google.maps.MapTypeId.ROADMAP,地图类型控制:假,地图类型控制选项:{中心:真实,缩放:10,},}},标记:{值:标记,选项:{可拖动:假},事件:{单击:函数(标记、事件、上下文){$('#map-modal').empty().append(context.data).modal('show');$(this).gmap3('get').panTo(marker.getPosition());},},},自动调整:{maxZoom:16},})});聚合物({是:'结果图',//创建元素实例时触发创建:函数(){},//当本地 DOM 已经完全准备好时触发准备好:函数(){},//当元素被插入到文档中时触发附:函数(){},//当元素从文档中移除时触发分离:函数(){},//添加、删除或更新属性时触发属性更改:函数(名称,类型){}});</dom-module>

这背后的想法是地图标记必须在循环服务器端制作,所以我然后有时在整个站点中标记是不同的,然后将它们加载到一个通用的 webcomponent 并推送标记进入标记"对象.

当我最初编写脚本时,我在 document.ready 上触发,这将导致 IE 和 FF 不加载标记,因为在加载组件之前不会创建 VAR,因此更改为 HTMLImports.whenReady 这允许这样做会发生,但现在由于某种原因破坏了 chrome.

重新阅读我的问题,我想具体一点,Chrome 中的对象标记为空,但 IE 中的 FF 中没有.

解决方案

当您在 Chrome 中注册对 HTMLImports.whenReady 的回调时,该事件已经触发.您的回调将不会被调用,因为它注册得太晚了.使用您的解决方案,您可以在第一个分配给 markers 的 Chrome 和第二个的 polyfill 浏览器中找到 Chrome.通常处理这个问题的另一种方法是:

if (HTMLImports.ready) {做东西();} 别的 {HTMLImports.whenReady(doStuff);}

这样,您可以确保 doStuff 函数只会被调用一次.

I have a web component using HTMLImports.whenReady to make sure that certain things get fired in the correct timing, and the problem is this fixed the bugs I was having in IE and Firefox, but now it is not working in chrome.

so on the page of the site i have this:

<link rel="import" href="/static/template/components/result-map.html">
<link rel="import" href="/static/template/components/result-options.html">
<link rel="import" href="/static/template/components/advanced-modal.html">
<link rel="import" href="/static/template/components/map-modal.html">

<div class="row-fluid">
    <div id="component" class="span12">
        <div class="well">
            <!-- TMPL_IF Listings -->
            <div class="row-fluid">
                <small><em>Press a map marker for more information</em></small><br />
                <result-map></result-map>
            </div>
            <!-- /TMPL_IF -->
        </div>
    </div>
</div>

<script>    
/*GENERATE MAP MARKERS*/
HTMLImports.whenReady(function(){
    <!-- TMPL_IF Listings -->   
        <!-- TMPL_LOOP Listings -->
            <!-- TMPL_IF have_geocode -->
                markers.push({
                    latLng: [<!-- TMPL_VAR latitude -->, <!-- TMPL_VAR longitude -->],
                    data: '...',
                    options: {
                        icon: "/static/images/mapmarker.png",
                        title: "<!-- TMPL_VAR street_no --> <!-- TMPL_VAR street -->, <!-- TMPL_VAR city -->, <!-- TMPL_VAR state --> <!-- TMPL_VAR zip -->"
                    }
                });
            <!-- TMPL_ELSE -->
                markers.push({
                    address:"<!-- TMPL_VAR street_no --> <!-- TMPL_VAR street -->, <!-- TMPL_VAR city -->, <!-- TMPL_VAR state --> <!-- TMPL_VAR zip -->",
                    data: '...',
                    options: {
                        icon: "/static/images/mapmarker.png",
                        title: "<!-- TMPL_VAR street_no --> <!-- TMPL_VAR street -->, <!-- TMPL_VAR city -->, <!-- TMPL_VAR state --> <!-- TMPL_VAR zip -->"
                    }
                });
            <!-- /TMPL_IF -->
        <!-- /TMPL_LOOP -->
    <!-- /TMPL_IF -->
});
</script>

and then the polymer webcomponent looks like this

<dom-module id="result-map">
<template>
    <div id="map_canvas"></div>
</template>

<script>
var markers = [];

HTMLImports.whenReady(function(){
    $('#map_canvas').gmap3({
        map:{
            options:{ 
                streetViewControl: false,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: false,
                mapTypeControlOptions: {
                center: true,
                zoom: 10,
            },
         }
        },
        marker:{
            values: markers,
            options:{
                draggable: false
            },
            events:{
                click: function(marker, event, context){
                    $('#map-modal').empty().append(context.data).modal('show');
                    $(this).gmap3('get').panTo(marker.getPosition());
                },
            },
        },
        autofit:{maxZoom: 16},
    })
});

Polymer({
    is: 'result-map',

    // Fires when an instance of the element is created
    created: function() {},

    // Fires when the local DOM has been fully prepared
    ready: function() {},

    // Fires when the element was inserted into the document
    attached: function() {},

    // Fires when the element was removed from the document
    detached: function() {},

    // Fires when an attribute was added, removed, or updated
    attributeChanged: function(name, type) {}
});
</script>
</dom-module>

The idea behind this is that the map markers have to be made in the loop server side so I then, and at times through out the site the markers are different, and then load them in to a common webcomponent and push the markers into the "marker" object.

When I originally wrote the script I was firing on document.ready and this would cause IE and FF to not load the markers as the VAR was not created until the the component was loaded, so changing to HTMLImports.whenReady this allowed this to happen, but now breaks chrome for some reason.

Re-reading my question, I want to be specific, the object markers is empty in chrome but not in IE an FF with what is above.

解决方案

At the time you register your callback to HTMLImports.whenReady in Chrome, the event has already fired. Your callback won't get called because it was registered too late. With your solution, you catch Chrome in the first assignment to markers and the polyfilled browsers in the second one. Another way to handle this problem in general is this:

if (HTMLImports.ready) {
   doStuff();
} else {
   HTMLImports.whenReady(doStuff);
}

This way, you ensure that the doStuff function will be called exactly once.

这篇关于使用 HTMLImports.whenReady 在 chrome 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆