开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜