Dojo加載自定義對象

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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章