为AngularJS驱动的学习页面存储数据的正确方法是什么

时间:2022-08-24 22:17:35

I am building a webpage for learning. Actually doing the page is the main goal, if it works well it would only be a bonus since i will most likely be the only person using it.

我正在建立一个学习的网页。实际上,做页面是主要的目标,如果它运行得很好,这将只是一个奖励,因为我很可能是唯一使用它的人。

That being said i am using Angular Objects that hold a lot of informations, like:

也就是说,我使用的是有角的物体它们包含很多信息,比如:

Semester - Subcategory - Question - List of answers as objects with "true"/ "false" properties for multi choice and the answer itself ect.

学期-子类别-问题-以“真”/“假”属性为对象的答案列表,用于多选择和答案本身等。

Since i will be doing the whole sorting / filtering with angular i wonder if i really need SQL or if a XML file would be superior.

由于我将使用角度进行整个排序/过滤,我想知道我是否真的需要SQL或XML文件是否更好。

With SQL saving is my main issue here. PHP seems to butcher arrays into a string with the value "array". If i use json_encode it saves correctly, but on GET it stops working since i have to rebuild the whole data structure with " and ' about everywhere.

SQL保存是我这里的主要问题。PHP似乎用值“array”将数组转换为字符串。如果我使用json_encode,它会正确地保存,但在GET上它就停止工作了,因为我必须用“about everywhere”重新构建整个数据结构。

With XML it really looks like angular just is not build for that. I have found some outdated tutorials that did not even have a working example.

对于XML,它看起来确实是有棱角的。我发现一些过时的教程甚至没有一个工作示例。

So i guess my question here is: Do i either go for SQL, putting up with multiple tables. Splitting my objects into several columns with optional values all over the place, while also rebuilding the whole thing on load?

所以我想我的问题是:我是否会选择SQL,使用多个表。将我的对象分割成几个列,并在加载时重新构建整个对象?

Or do i use XML, since i would only use the DB to GET the whole thing anyways?

或者我使用XML,因为我只会用DB来得到整个东西?

Both approaches have been tested by me and work, somewhat. Both would need quite a lot of further digging, reading and trying. I don't have the spare time to do both routes. Which one is the better one to go for in this particular use case?

这两种方法都经过了我和工作的测试。两者都需要更多的挖掘、阅读和尝试。我没有空闲时间做这两条路。在这个特定的用例中,哪个更好?

2 个解决方案

#1


2  

This is ofcourse a personal preference but I always try to avoid XML. The JSON format is alot lean and meaner and it's way easier to work with in web applications.

这当然是我个人的偏好,但我总是尽量避免使用XML。JSON格式非常精练,而且在web应用程序中更容易使用。

In fact I would suggest to start with some static JSON files until you're finished with giving your website some structure. You can generate them manually, use some generator tools (like http://www.mockaroo.com/) or build them by using some simple javascript (JSON.stringify is your friend). You can then use this data quite easily by using the $http service:

事实上,我建议您从一些静态JSON文件开始,直到您完成给您的网站一些结构。您可以手动生成它们,使用一些生成器工具(如http://www.mockaroo.com/),或者使用一些简单的javascript (JSON)构建它们。stringify是你的朋友)。您可以使用$http服务轻松地使用这些数据:

$http.get('my-data.json')
  .then(function(response) {
    $scope.myData = response.data;
  });

This is actually the approach my teams take when building large enterprise applications. We mock all data resources and replace them with the real thing when we (or the customer) are happy with the progress.

这实际上是我的团队在构建大型企业应用程序时所采用的方法。我们嘲笑所有的数据资源,当我们(或客户)对进展感到满意时,用真实的东西替换它们。

#2


1  

Using a JSON-File should be sufficient. You can store all the needed objects in it and change it easily. With the following code you can load the data within JavaScript

使用json文件就足够了。您可以在其中存储所有需要的对象并轻松地更改它。使用以下代码,您可以在JavaScript内加载数据

function loadJSON(path, success, error) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                if (success)
                    success(JSONH.parse(xhr.responseText));
            } else {
                if (error)
                    error(xhr);
            }
        }
    };
    xhr.open("GET", path, true);
    xhr.send();
}

usage

使用

loadJSON('data.json',//relative path
    function (data) {//success function
        $scope.questions = question;
        $scope.$apply();
    },
    function (xhr) {//error function
        console.error(xhr);
    }
);

#1


2  

This is ofcourse a personal preference but I always try to avoid XML. The JSON format is alot lean and meaner and it's way easier to work with in web applications.

这当然是我个人的偏好,但我总是尽量避免使用XML。JSON格式非常精练,而且在web应用程序中更容易使用。

In fact I would suggest to start with some static JSON files until you're finished with giving your website some structure. You can generate them manually, use some generator tools (like http://www.mockaroo.com/) or build them by using some simple javascript (JSON.stringify is your friend). You can then use this data quite easily by using the $http service:

事实上,我建议您从一些静态JSON文件开始,直到您完成给您的网站一些结构。您可以手动生成它们,使用一些生成器工具(如http://www.mockaroo.com/),或者使用一些简单的javascript (JSON)构建它们。stringify是你的朋友)。您可以使用$http服务轻松地使用这些数据:

$http.get('my-data.json')
  .then(function(response) {
    $scope.myData = response.data;
  });

This is actually the approach my teams take when building large enterprise applications. We mock all data resources and replace them with the real thing when we (or the customer) are happy with the progress.

这实际上是我的团队在构建大型企业应用程序时所采用的方法。我们嘲笑所有的数据资源,当我们(或客户)对进展感到满意时,用真实的东西替换它们。

#2


1  

Using a JSON-File should be sufficient. You can store all the needed objects in it and change it easily. With the following code you can load the data within JavaScript

使用json文件就足够了。您可以在其中存储所有需要的对象并轻松地更改它。使用以下代码,您可以在JavaScript内加载数据

function loadJSON(path, success, error) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                if (success)
                    success(JSONH.parse(xhr.responseText));
            } else {
                if (error)
                    error(xhr);
            }
        }
    };
    xhr.open("GET", path, true);
    xhr.send();
}

usage

使用

loadJSON('data.json',//relative path
    function (data) {//success function
        $scope.questions = question;
        $scope.$apply();
    },
    function (xhr) {//error function
        console.error(xhr);
    }
);