正在学requirejs,写了个小Demo,不知为何提示其中一个依赖undefined:
这个依赖是widget.js,代码如下:
// 自定义Widget抽象类 define(['jquery'],function($){ function Widget(){ // 最外层容器 this.boundingBox = null; } Widget.prototype = { // type 为自定义类型,handler 为处理函数 // 开启监听的方法 on:function (type,handler) { if(typeof this.handlers[type] == 'undefined'){ this.handlers[type] = []; } this.handlers[type].push(handler); // 连缀语法 return this; }, // 触发事件的方法,data为执行具体某个参数时可能会用到的参数,可以为空 fire:function (type,data) { if(this.handlers[type] instanceof Array){ var handlers = this.handlers[type]; for (var i = 0; i < handlers.length; i++) { handlers[i](data); }; } } render:function(container) { this.renderUI(); this.handlers={}; this.bindUI{}; this.syncUI{}; $(container||document.body).append(this.boundingBox); }, destroy:function () { this.destructor(); this.boundingBox.off(); this.boundingBox.remove(); }, renderUI:function () {}, bindUI:function () {}, syncUI:function () {}, destructor:function () {}, } return { Widget:Widget } });
require.config({ paths:{ // 路径值去掉js后缀文件名 jquery:'jquery-2.1.0.min', jqueryUI:'http://libs.baidu.com/jqueryui/1.10.2/jquery-ui.min' } }); // 程序入口 require(['widget','jquery'],function (w,$) { $(function() { alert(new w.Widget()); }); });
fire 函数后少个逗号。