从JS函数返回

时间:2012-04-17 20:48:37

标签: javascript dojo esri

基本的JS问题,请放轻松我就是新手:)

我将2个变量传递给findRelatedRecords函数,该函数查询其他相关表并组装一个对象数组,称为数据。由于findRelatedRecords有很多内部函数,我很难从数据中获取数据。

就目前而言,我在findRelatedRecords中调用showWin,但我想更改它以便我可以直接从findRelatedRecords获取数据Array,而不是跳转到showWin

function findRelatedRecords(features,evtObj){
    //first relationship query to find related branches

    var selFeat = features
    var featObjId = selFeat[0].attributes.OBJECTID_1        
    var relatedBranch = new esri.tasks.RelationshipQuery();
    relatedBranch.outFields = ["*"];
    relatedBranch.relationshipId = 1; //fac -to- Branch
    relatedBranch.objectIds = [featObjId];
        facSel.queryRelatedFeatures(relatedBranch, function(relatedBranches) {
        var branchFound = false;
        if(relatedBranches.hasOwnProperty(featObjId) == true){
            branchFound = true;
            var branchSet = relatedBranches[featObjId]
            var cmdBranch = dojo.map(branchSet.features, function(feature){
                return feature.attributes;
            })
        }

        //regardless of whether a branch is found or not, we have to run the cmdMain relationship query
        //the parent is still fac, no advantage of the parent being branch since cmcMain query has to be run regardless
        //fac - branch - cmdMain - cmdSub <--sometimes
        //fac - cmdMain - cmdSub <-- sometimes

        //second relationship query to find related cmdMains            
        var relatedQuery = new esri.tasks.RelationshipQuery();
        relatedQuery.outFields = ["*"];
        relatedQuery.relationshipId = 0; //fac -to- cmdMain
        relatedQuery.objectIds = [featObjId];
        //rather then listen for "OnSelectionComplete" we are using the queryRelatedFeatures callback function
        facSel.queryRelatedFeatures(relatedQuery, function(relatedRecords) {
            var data = []           
            //if any cmdMain records were found, relatedRecords object will have a property = to the OBJECTID of the clicked feature
            //i.e. if cmdMain records are found, true will be returned; and continue with finding cmdSub records
            if(relatedRecords.hasOwnProperty(featObjId) == true){
                var fset = relatedRecords[featObjId]
                var cmdMain = dojo.map(fset.features, function(feature) {
                    return feature.attributes;
                })
                //we need to fill an array with the objectids of the returned cmdMain records
                //the length of this list == total number of mainCmd records returned for the clicked facility
                objs = []
                for (var k in cmdMain){
                    var o = cmdMain[k];
                    objs.push(o.OBJECTID)                       
                }

                //third relationship query to find records related to cmdMain (cmdSub)
                var subQuery = new esri.tasks.RelationshipQuery();
                subQuery.outFields = ["*"];
                subQuery.relationshipId = 2;
                subQuery.objectIds = [objs]
                subTbl.queryRelatedFeatures(subQuery, function (subRecords){

                    //subRecords is an object where each property is the objectid of a cmdMain record
                    //if a cmdRecord objectid is present in subRecords property, cmdMain has sub records
                    //we no longer need these objectids, so we'll remove them and put the array into cmdsub
                    var cmdSub = []
                    for (id in subRecords){
                        dojo.forEach(subRecords[id].features, function(rec){
                            cmdSub.push(rec.attributes)
                        })
                    }
                    var j = cmdSub.length;
                    var p;
                    var sub_key;
                    var obj;
                    if (branchFound == true){
                        var p1 = "branch";
                        obj1 = {};
                        obj1[p1] = [cmdBranch[0].Branches]
                        data.push(obj1)
                    }                        
                    for (var i=0, iLen = cmdMain.length; i<iLen; i++) {
                        p = cmdMain[i].ASGMT_Name
                        obj = {};
                        obj[p] = [];
                        sub_key = cmdMain[i].sub_key;
                        for (var j=0, jLen=cmdSub.length; j<jLen; j++) {
                            if (cmdSub[j].sub_key == sub_key) {
                                obj[p].push(cmdSub[j].Long_Name);
                            }
                        }
                        data.push(obj);
                    }
                    showWin(data,evtObj) <---this would go away
                })
            }
            //no returned cmdRecords; cmdData not available
            else{
                p = "No Data Available"
                obj = {}
                obj[p] = []
                data.push(obj)
            }
            showWin(data,evtObj) <--this would go away
        })
    })
}

我想通过调用

来访问数据数组
function findRelatedRecords(feature,evt){
    //code pasted above
}

function newfunct(){
    var newData = findRelatedRecords(feature,evt)
    console.log(newData)
}

这可能吗?

谢谢!

修改

更多解释.....

我正在将Object事件侦听器连接到函数,如下所示:

function b (input){
    dojo.connect(obj, "onQueryRelatedFeaturesComplete", getData);
    obj.queryRelatedFeatures(input);
    console.log(arr) //<----this doesn't work
}

function getData(relatedFeatData){
    var arr = [];
    //populate arr
    return arr;
}

所以当obj.QueryRelatedFeatures()完成时,getData会触发;这部分工作正常,但如何从函数b访问arr?

1 个答案:

答案 0 :(得分:1)

发布编辑更新:

由于连接此事件的方式,您不能简单地从中返回数据。返回将让Dojo调用连接到onSelectionComplete的下一个方法。

init运行时,井findRelatedRecords事件将onSelectionComplete执行/触发之前很久,这就是您看到undefined / {的原因{1}}值。使用这种系统的唯一方法是:1)调用你正在做的方法或2)触发自定义事件/消息(技术上它仍然只是调用方法)。

如果你想让这个方法更容易使用,你应该重构/提取它的片段,使它成为一个较小的函数,但包含在许多函数中。此外,将其更改为null方法末尾只有一个退出点将有所帮助。在findRelatedRecords内定义的函数将是伟大的开始的地方。

很抱歉,在这种情况下,你会受到Dojo给你的限制。

预编辑答案:

只需将数据退出即可。在有showWin调用的地方,只需使用此返回。

subTbl.queryRelatedFeatures()

然后你的新手就像这样。

  return {
    data: data,
    evtObj: evtObj
  }

如果您只需要该“数据”对象,请将返回值更改为function newfunct(){ var newData = findRelatedRecords(feature,evt); console.log(newData); console.log(newData.data); console.log(newData.evtObj); }

此外,请开始使用分号来终止语句。