Save modified json file using AngularJS

I am creating a phonegap app with AngularJS that uses a .json file to store an array of entries. My goal is to allow the user to mark some of them as favorites and then use that data somewhere else (for example, another page with only favorites). I am doing web development first, and I must say that I am fairly new to web technologies like AngularJS, JSON, PhoneGap, etc. (And I use them all) so maybe this question is nonsense, but I don't care here.

I tried to store this data in IndexedDB in a WebSQL database in WebStorage .... but these approaches seem to be tricky when combined with PhoneGap, so I am now trying to change the field in. json and save the modified file.

I know JS cannot write files, but I was trying to send a POST file instead to fill that gap. Here's the code for the function I'm using:

$scope.save = function() {

    $http({
        method: 'POST',
        url: 'data.json',         //this is the json file with all the information I use
        data: $scope.json_data    //this contains the modified data
    }).success(function(response) {
        addLog("Success message.");
    }).error(function(response){
        addLog("Error message.");
    });
}

      

I don't want to write a lot of code to handle the server side of the requests (I don't even know if PhoneGap can handle it ...) since I honestly don't even know where to start what. I don't know if transformRequest or some other simple thing can let me save the json file ... Any ideas on what to do? Should I save the modified .json file with some magic code, or maybe I am looking at this the wrong way and should go the other way?

Thank!

+3


source to share


1 answer


You have several options that you could use.

  • HTML5 localStorage

    API. This would probably be the easiest. It is key value based, with string keys and string values. It's also extremely portable. Depending on how simple the application is, this might be the best. You set elements as a property window.localStorage

    or using getKey

    / setKey

    . There is a ton of documentation and tutorials about this on the internet.
    • Caveat: In Windows Phone 7, you cannot use dot notation. You should use the latter method.
  • Use the Cordova File API. It is less consistent across operating systems than it is localStorage

    , but it is still quite reliable. Here's a link for more information.
    • Caution . Besides the incompatibility between the operating system, you can also install it as a plugin. Here the team: cordova plugin add org.apache.cordova.file

      .


Here are some useful links:

+5


source







All Articles