使用Ember数据从发布请求中检索服务器响应中的JSON有效负载

时间:2015-06-18 02:19:01

标签: json ember.js ember-data

我一定是真的遗漏了一些东西,但我找不到使用ember数据从post请求中检索json有效负载的方法。例如:

//TODO: Send exception to LOGGER to print to console

package model;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public class ApplicationFileReader {

    public String filePathLocation;
    public String fileText;
    public ArrayList<String> fileTextLines;

    public ApplicationFileReader(){
        filePathLocation = "";
        fileText = "";
        fileTextLines = new ArrayList<String>();
    }

    public ApplicationFileReader(String filePathLocation){
        this.filePathLocation = filePathLocation;
        this.fileText = "";
        fileTextLines = new ArrayList<String>();
        extractFileText(filePathLocation);

        /* Testing */
        for(String s : fileTextLines)
            System.out.println(s);
    }

    public boolean extractFileText(String filePathLocation) {
        if(!validateFilePathLocation(filePathLocation))
            return false;
        try {
            Path path_filePath = Paths.get(filePathLocation);
            fileTextLines.addAll(Files.readAllLines(path_filePath, StandardCharsets.UTF_8));
            for(String eachListLine : fileTextLines){
                fileText += eachListLine;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    private boolean validateFilePathLocation(String filePathLocation) {
        try {
            File temporaryFile = new File(filePathLocation);
            if(!temporaryFile.exists() && temporaryFile.isDirectory())
                throw new IOException();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

Ember Data自动处理来自POST的响应,并插入和/或替换具有匹配ID的任何对象。对象从有效负载中自动提取并放置在商店中。 Read about Ember Data's REST adapter and the conventions expected of your server