开发者

dojo ItemFileReadStore.getValue mixed return value is not handled as string

开发者 https://www.devze.com 2023-02-21 19:45 出处:网络
I\'am using dojo.data.ItemFileReadStore to query a json file with data. the main purpose is finding translations at Js level.

I'am using dojo.data.ItemFileReadStore to query a json file with data. the main purpose is finding translations at Js level.

The Json data has "id" the word and "t" the translation

function translate(word)
{
var json = '/my/language/path/es.json';    
var reader = new dojo.data.ItemFileReadStore({
    url: json
});
var queryObj = {};

queryObj["id"] = word;

reader.fetch({
    query: queryObj,        
    onComplete: function(items, request){
        if (items.length > 0) {
            var t = reader.getValue(items[0], 't');
            if (dojo.isString(t)) {                    
                return t;
            }
        }            
        return word;
    },
    onError: function(error, request){            
        return word;
    }
});    
}

The return value is always a开发者_如何学C undefined wether there is a translation or not. any ideas? I tried typecasting with no success.


You can do it like this:

function translate(wordId) {

  var translatedWord= wordId;

  var store = new dojo.data.ItemFileReadStore({ data: storeData });

  store.fetch({ query: { id: wordId },
                    onItem: function (item) {
                        translatedWord= (store.getValue(item, 't'));
                    }
                });

  return translatedWord;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消