Dojo加载自定义对象

来源:互联网 发布:跑步计步器软件 编辑:程序博客网 时间:2024/05/05 09:19

Dojo加载自定义对象的一种方法

定义有如下对象:

./js/com/WebGIS/Graphics/Shape.js中:

define(['dojo/_base/declare'], function (declare) {
    return declare(
        null,
        {
            color: 0,
            setColor: function (color) {
                this.color = color;
            }
        });
});

./js/com/WebGIS/Graphics/Circle.js中:

define(["dojo/_base/declare", "com/WebGIS/Graphics/Shape"], function (declare, Shape) {
    return declare(
        Shape,
        {
            constructor: function (radius) {
                this.radius = radius | this.setRadius;
            },
            setRadius: function (radius) {
                this.radius = radius;
            },
            area: function () {
                return Math.PI * this.radius * this.radius;
            }
        });
});

加载文件时:

    <script src="http://localhost:8080/dojoroot/dojo/dojo.js"></script>    //加载dojo

    <script>
        (function () {    
            var currentPath = location.href.substring(0, location.href.lastIndexOf("\/"));
            require({     //定义加载当前块环境需要的包名称定义
                packages: [{ name: "com", location: currentPath + "/js/com" }]
            })
            require(["com/WebGIS/Graphics/Circle", "dojo/domReady!"], function (Circle) {
                var circle = new Circle(4);
                console.log(circle.area());
                console.log(circle.color);
            });
        }());
    </script>

0 0
原创粉丝点击