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>