简体   繁体   English

如何使用Javascript覆盖我的甾体js应用程序中的本地JSON文件?

[英]How can I overwrite the local JSON file in my steroids js application with Javascript?

I'm building a steroids js app and it uses data from a local json file in the www/data folder under "MyApp.json" to display things like a thumbnail image, a description etc. I want to overwrite this information upon launch of my application each time with JSON data from my server using JOSNP. 我正在构建类固醇js应用程序,它使用“ MyApp.json”下www / data文件夹中本地json文件中的数据来显示缩略图,说明等内容。我想在启动时覆盖这些信息我的应用程序每次都使用JOSNP从服务器获取JSON数据。 I just need to know if it's possible for me to overwrite my local JSON file each time my app launches with the JSON data from my server. 我只需要知道每次我的应用程序使用服务器上的JSON数据启动时是否可以覆盖本地JSON文件。 If so, how would I go about overwriting this file and would it work across platforms (ios, android, windows)? 如果是这样,我将如何覆盖此文件,并且它可以在各种平台(iOS,Android,Windows)上运行?

If I understand right, you want a offline mode with online sync. 如果我理解正确,则您需要具有在线同步功能的脱机模式。

Best thing to do for you is to store in localStorage your javascript object 最好的办法是将javascript对象存储在localStorage中

// json to storage 
localStorage.myList = JSON.stringify( list );
// storage to json
var list = JSON.parse( localStorage.myList );

The mozilla foundation have just released this weak a framework that handle different kind of online storage (asynchrounous db) : Mozilla基金会刚刚发布了这个弱框架,用于处理不同类型的在线存储(异步db):

https://github.com/mozilla/localForage 

You can on each load call a refresh function that get the data from the server and store it in local, if the app is offline, directly run the script from the local database 您可以在每次加载时调用刷新功能,该刷新功能从服务器获取数据并将其存储在本地,如果应用程序处于脱机状态,则直接从本地数据库运行脚本

2this is not possible with client side javascripts, you need some sort of backend for this applicaiton - i mean a PHP script, that can change the JSON file or nodejs script. 2这对于客户端JavaScript来说是不可能的,您需要此应用程序的某种后端-我的意思是一个PHP脚本,可以更改JSON文件或nodejs脚本。

The easiest idea is to make a PHP script and name if jsondata.php 最简单的想法是制作一个PHP脚本并命名为jsondata.php

something like it 像这样的东西

if($_POST['data']){
  $handle = fopen('data.json', 'a');
  fwrite($handle, $_POST['data'])
  fclose($handle);
} else {
 readfile('data.json');
}

When you issue a GET call to this script, the data is given When you issue the POST call to this script, the data is saved 对此脚本发出GET调用时,将给出数据。对此脚本发出POST调用时,将保存数据。

Sorry, i haven't wrote scripts on PHP for about two years, so this script is lame, but i hope you got the idea 对不起,我已经有两年没有用PHP编写脚本了,所以这个脚本很me脚,但是我希望你能理解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM