English 中文(简体)
重构大 JSON 多次呼叫
原标题:Refactoring big JSON multiple calls

我正在撰写 java/ spring 网络应用程序, 每个用户都可以在其中拥有自己的单词列表。 每次用户添加/ 删除一个单词时, 都会有一个 ajax 呼叫, 发送 JSON 来添加/ 删除此单词, 当服务器侧面完成后, 将 JSON 对象中的当前单词列表还给 jQuery 。

对小 JSON 单词列表来说, 添加和删除的速度非常快, 但当单词列表变大时, Ajax 调用速度就会变慢。 添加/ 删除调用将会非常频繁 。

JSON文字列表示例[polish words]:

 [{"word":"abrys","wordId":646},{"word":"acpan","wordId":647},
 {"word":"akrania","wordId":457},{"word":"akwawita","wordId":648},
 {"word":"amalgamat","wordId":399},{"word":"amurski","wordId":465},
 {"word":"amurskie","wordId":1030},{"word":"ananke","wordId":649},
 {"word":"androlog","wordId":650}]

jj查询 [或多或少]:

$("#add_word_submit").live("click", function(e) {
    e.preventDefault();
    var word = $( #add_word ).serializeObject();
    $.postJSON("word.html", word, function(words) {
        showDividedAccountWords(words);     
    });
});

$(".delete_button").live("click", function(e) {
    e.preventDefault();
    if(confirm(Main.confirmWordDelete)) {
        var wordId = $(this).attr("id").substring("delete_".length);
        wordName["word"] = $(this).children("img").attr("alt");
        $.ajax({
            type: "DELETE",
            url: "word/" + wordId + ".html",
            success: function(words) {
                showDividedAccountWords(words);
            }
        }); 
    }
});

爪哇/春春:

@RequestMapping(value="/word", method = RequestMethod.POST)
public @ResponseBody List<Word> addNewWord(@RequestBody Word word, Principal principal) {
    wordService.addNewWord(word, principal.getName());
    return getCurrentWords(principal.getName());
}

@RequestMapping(value="/word/{wordId}", method = RequestMethod.DELETE)
public @ResponseBody List<Word> deleteWord(@PathVariable Long wordId, Principal principal) {
    wordService.deleteWord(wordId, principal.getName());
    return getCurrentWords(principal.getName());
}

public List<Word> getCurrentWords(String username) {
    List<Word> accountWords = new ArrayList<Word>();
    accountWords.addAll(wordService.listUserWords(username));
    return accountWords;
}

您能否给我一些提示来重构它? 或许每次用户添加/删除单词时,我不应该返回当前单词列表?

最佳回答

要将单词对象递增添加到列表中,请将新单词传递到服务器,然后只返回一个(字:wordid)对象。然后,您可以将其添加到现有列表中,并按以下排序:

var words=[{"word":"abrys","wordId":646},{"word":"acpan","wordId":647},
 {"word":"akrania","wordId":457},{"word":"akwawita","wordId":648},
 {"word":"amalgamat","wordId":399},{"word":"amurski","wordId":465},
 {"word":"amurskie","wordId":1030},{"word":"ananke","wordId":649},
 { "word": "androlog", "wordId": 650}];


var result = { "word": "aklina", "wordid": "702" }; // returned by server from AJAX call
words.push(result); 

words.sort(
  function(a, b) {
    return a.word.localeCompare(b.word);
  }
 );
问题回答

我会使用 java/spring 处理信息, 但只返回一个小字符串, 以检查数据传输是否延迟。 如果在 java/ spring 处理数据方面没有重大区别, 那么它要么在传输或jquery 处理中。 您可以测试 jQuery 是否在传输完成后向控制台发出警告 。 如果是快速的, 那么必须是 jQuery 处理更长的数据 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签