首页 > 解决方案 > sapUI5/OpenUI5 Leafletjs 自定义控件

问题描述

我使用库中的自定义控件在 openUI5 和 Leafletjs 中遇到了这个问题。

错误:

“类 demo.map.SimpleMap 的渲染器未定义或未定义渲染函数!将跳过 __map0 的渲染!”...

库.js

sap.ui.define([
'jquery.sap.global',
'sap/ui/core/library'],
function(jQuery){
                "use strict";
                sap.ui.getCore().initLibrary({
                        name: 'demo.map',
                        version: '1.0.0',
                        dependencies: ['sap.ui.core'],
                        types: [],
                        interfaces: [],
                        controls:[
                                'demo.map.SimpleMap'
                        ],
                        elements:[]
                });
                return demo.map;
        });

SimpleMap.js

sap.ui.define([
'jquery.sap.global',
'sap/ui/core/Control', 
'./library'], function(jQuery, Control, library){
                    "use strict";

                    let SimpleMap = Control.extend('demo.map.SimpleMap',{
                            metadata:{
                                    library: 'demo.map',
                                    properties:{}
                            }
                    });

                    SimpleMap.prototype.drawMap = function(){
                            this.controlAspect = parseInt(450) / parseInt(350);
                            let map = L.map('map').setView([39.7166700,-8],8);
                            L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'}).addTo(map);
                    }

                    SimpleMap.prototype.onAfterRendering = function(){
                            this.drawMap();
                    }

                    return SimpleMap;
            }, true);

SimpleMapRenderer.js

sap.ui.define(['jquery.sap.global'], function(jQuery){
            "use strict";

            let SimpleMapRenderer = {};

            SimpleMapRenderer.renderer = function(oRm, oControl){
                    oRm.write('<div ');
                    oRm.writeControlData(oControl);
                    oRm.write('>');
                    oRm.write('</div>');
            }

            return SimpleMapRenderer;
    });

起始页.view.xml

<mvc:View controllerName="sap.ui.demo.fiori.controllers.Startpage" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" xmlns:layout="sap.ui.layout">
    <Page title="Fiori Tile Demo">
            <layout:VerticalLayout class="sapUiResponsiveMargin">
                    <Title titleStyle="H2" text="Launchpad Menu" class="sapUiTinyMarginBegin"/>
                    <layout:HorizontalLayout allowWrapping="true" id="layout">

                    </layout:HorizontalLayout>
            </layout:VerticalLayout>
    </Page>
</mvc:View>

起始页.controller.js

    sap.ui.define(['sap/ui/demo/fiori/controllers/BaseController'], function(Controller){
                "use strict";
                return Controller.extend('sap.ui.demo.fiori.controller.Startpage',{
                        onInit:function(){
                                console.log('Startpage loaded');
                                let map = new demo.map.SimpleMap();
                                //console.log(map);
                                let oLay = this.getView().byId('layout');
                                oLay.addContent(map);
                        },
                        gotoUserList: function(){
                                this.getRouter().navTo('listUsers');
                        },
                        getRouter: function(){
                                return this.getOwnerComponent().getRouter();
                        }
                });
        });

另外,我尝试直接从控制器添加地图对象而没有自定义控件,但出现以下错误

Leafletjs 框架中的“找不到地图容器”错误。

希望有人请帮助我。我很迷茫如何使用 openUI5 渲染传单。

标签: leafletsapui5

解决方案


好的,这就是我如何让它工作:

SimpleMap.js

sap.ui.define(['jquery.sap.global','sap/ui/core/Control', './library'], function(jQuery, Control, library){
        "use strict";

        let SimpleMap = Control.extend('demo.map.SimpleMap',{
            metadata:{
                library: 'demo.map',
                properties:{
                    "width":{type: 'sap.ui.core.CSSSize', defaultValue:'300px'},
                    "height":{type: 'sap.ui.core.CSSSize', defaultValue:'300px'}
                }
            }
        });

        SimpleMap.prototype._drawMap = function(){
            this.controlAspect = parseInt(300) / parseInt(300);
            /* using: L.map(this.$()).setView(...); << trown an error: 
            "Cannot read property 'baseVal' of undefined", which seems that is a jQuery object instead of a DOM element. Ref: 
            https://github.com/Leaflet/Leaflet/issues/2302 
            I couldn't how to get current DOM element.
            */
            let map = L.map('map').setView([39.7166700,-8],8);
            L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'}).addTo(map);
        }

        SimpleMap.prototype.onAfterRendering = function(){
            this._drawMap();
        }

        return SimpleMap;
    }, true);

SimpleMapRenderer.js

sap.ui.define(['jquery.sap.global'], function(jQuery){
        "use strict";

        let SimpleMapRenderer = {};

        SimpleMapRenderer.render = function(oRm, oControl){
            oRm.write('<div ');
            oRm.write('id="map"'); // set it hardcoded
            oRm.writeControlData(oControl);
            oRm.addStyle('height',oControl.getHeight());
            oRm.addStyle('width',oControl.getWidth());
            oRm.writeStyles();
            oRm.write('>');
            oRm.write('</div>');
        }

        return SimpleMapRenderer;
    }, true); // notice this: last version i did not export it...

感谢你的帮助。

PS.-而不是使用 oRm.write('id="map"'); << 并从控制如何使用 this.$() 获取 dom 元素?我试过了: this.$()[0] 但什么都没有......


推荐阅读