dojo声明正确的方式

编程入门 行业动态 更新时间:2024-10-15 14:13:59
本文介绍了dojo声明正确的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

文件: dojo / dir1 / utils / XmlJsonUtils.js

// Author: Rajat Khandelwal define([ "dojo/_base/declare" // declare ], function(declare){ return declare("dir1.utils.XmlJsonUtils",[],{ parseXml : function (xml) { var self=this; var dom = null; if (window.DOMParser) { try { dom = (new DOMParser()).parseFromString(xml, "text/xml"); } catch (e) { dom = null; } } else if (window.ActiveXObject) { try { dom = new ActiveXObject('Microsoft.XMLDOM'); dom.async = false; if (!dom.loadXML(xml)) // parse error .. window.alert(dom.parseError.reason + dom.parseError.srcText); } catch (e) { dom = null; } } else alert("cannot parse xml string!"); return dom; }, xml2json : function (xmldata) { var self=this; if(xmldata.firstChild==null) { return {name:xmldata.nodeName+": (value null)", checked: true} } else if(xmldata.firstChild.nodeType==3) { return {name:xmldata.nodeName+": "+xmldata.firstChild.nodeValue, checked:true} } else { var mychildren=[]; var i=0; var nochildren=xmldata.childElementCount for(i=0;i<nochildren;i++) { var j=self.xml2json(xmldata.childNodes[i]) mychildren[i]=j } var ret= {name:xmldata.nodeName, children:mychildren, checked:true} return ret } }, convert2arr : function (result,ctr,res_arr) { var self=this; if(result[ctr].checked[0]==false) return; if(result[ctr].children==undefined) { var name=result[ctr]['name'][0]; var kv = name.split(': '); if(kv[1]=="(value null)") kv[1]=""; res_arr.push.apply(res_arr,["<",kv[0],">",kv[1],"</",kv[0],">"]); return ctr+1; } else { var i=ctr; var new_ctr=ctr; var no_children=result[ctr].children.length; res_arr.push.apply(res_arr,["<",result[ctr].name[0],">"]) for(i=0;i<no_children;i++) { new_ctr=self.convert2arr(result,result[ctr].children[i]._0,res_arr) } res_arr.push.apply(res_arr,["</",result[ctr].name[0],">"]); return new_ctr; } }, convert2xml : function (result) { var arr=[] self.convert2arr(result, 0, arr) return arr.join('') } }) })

,但是在代码中需要 dir1.utils.XmlJsonUtils 时, code>未捕获错误:声明XmlJsonUtils:基类不是可调用构造函数。声明某些效用函数的正确方法是什么?

but when in the code I require the dir1.utils.XmlJsonUtils, it says Uncaught Error: declare XmlJsonUtils: base class is not a callable constructor. What is the correct way to declare some utility functions.

那些应该是静态函数。我不想做 x = new XmlJsonUtils(); x.parseXml(..)。我想做 XmlJsonUtils.parseXml(..)

And those should be like static functions. I don't want to do x=new XmlJsonUtils(); x.parseXml(..). I want to do XmlJsonUtils.parseXml(..)

推荐答案

你的类不应该有定义的构造方法,dojo.declare应该处理这个..但是,这样做没有伤害,只需定义一个空白的构造函数:function(){} 。我怀疑你面临某种错误。

Your class should not have to have the constructor method defined, dojo.declare is supposed to handle this.. However, doing so doesnt hurt, simply define a blank constructor: function() { }. I suspect youre facing some sort of bug.

定义是应该的,'define'用于require-scope,当运行require([我的。模块]),它预计有一个定义方法,它通过 declare 返回基类。

The define is as should be, 'define' is used for the require-scope, when running require(["my.module"]), its expected to have a define method, which returns the base class via declare.

文件: dojo / dir1 / utils / XmlJsonUtils.js :

define([ // requirements "dojo/_base/declare", "dir1/utils/Toolkit" // sample in-package dependency "./Toolkit" // Same as Above ], function (declare) { // no slash separator, use dot with declare, // use a reference and return on last line var Klass = declare( /// declaredClass: string, moduleUrl with dot-separater + filename /.js// "dir1.utils.XmlJsonUtils", /// base class: Array(mixins) [], /// class scope { _methodMeantToBePrivate: function() { }, randomInstanceMethod: function() { } } ); // end declare // set any aliases, which you want to expose (statics) Klass.StaticCallable = function() { // careful with your scope access inhere } // return the declared class to 'define' return Klass; }); // end define

这样(你必须有一个引用,或者用require或getObject来插入) ,您可以使用 StaticCallable 函数,而不初始化/构建模块的实例。 AMD兼容的语法是这样的:

This way (you must have a reference, either pulled in with require or getObject), you could use the StaticCallable function without initializing / constructing an instance of the module. AMD compliant syntax is like so:

require(["dir1/utils/XmlJsonUtils"], function(xmlUtils) { xmlUtils.StaticCallable(); });

或如果以前需要

var xmlUtils = dojo.getObject("dir1.utils.XmlJsonUtils") xmlUtils.StaticCallable();

一个具体的例子可能是一个通用的类,如下所示,实例和静态访问都是可能的。基类定义工具,派生类定义工具操作的变量,如果实例化,可以订阅默认主题 - [ MessageBusBase | MessageBus ]

A specific example could be a versatile class like the following, where both instance and static access is possible. Base class defines 'tools', derived class defines the variables the 'tools' operate on - and if instantiated, the default topics can be subscribed - [ MessageBusBase | MessageBus ]

更多推荐

dojo声明正确的方式

本文发布于:2023-10-28 04:30:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535525.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   声明   方式   dojo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!