嗨,我正在使用phonegap开发购物应用程序。我想给用户一个选择,以保存他们的订单,并完成他/她找到方便的时间。我的问题在哪里保存订单数据。移动设备的本地文件系统或本地数据库?我希望将json格式的订单 保存在本地文件中。请为我提供最好的选择。也是一个片段将高度赞赏。谢谢phonegap在本地文件系统保存订单
0
A
回答
1
您还可以使用HTML5 localStorage作为文件存储的更简单的替代方法。我一直在使用localStorage的封装版本来方便获取/设置操作并减少命名空间污染。请参阅下面的代码库:
/**
* The class is designed to facilitate flexible permanent storage of key value
* pairs utilzing HTML5 localStorage.
*
* @class LocalMap
* @author Zorayr Khalapyan
* @version 10/25/2012
*/
var LocalMap = function (name) {
var that = {};
//Prevent compatability issues in different execution environments.
if (!localStorage) {
localStorage = {};
}
if (!localStorage[name]) {
localStorage[name] = "{}";
}
var setMap = function (map) {
localStorage[name] = JSON.stringify(map);
};
that.getMap = function() {
return JSON.parse(localStorage[name]);
};
/**
* Stores the specified (key, value) pair in the localStorage
* under the map's namespace.
*/
that.set = function (name, object) {
var map = that.getMap();
map[ name ] = object;
setMap(map);
};
that.get = function (name) {
var map = that.getMap();
return typeof(map[ name ]) !== "undefined" ? map[name] : null;
};
that.importMap = function (object) {
var map = that.getMap();
var key;
for (key in object) {
if (object.hasOwnProperty(key)) {
map[key] = object[key];
}
}
setMap(map);
};
that.length = function() {
var map = that.getMap();
var size = 0, key;
for (key in map) {
if (map.hasOwnProperty(key)) size++;
}
return size;
};
that.erase = function() {
localStorage[name] = JSON.stringify({});
};
that.isSet = function (name) {
return that.get(name) != null;
};
that.release = function (name) {
var map = that.getMap();
if (map[name]) {
delete map[name];
}
setMap(map);
};
that.deleteNamespace = function(){
if (localStorage.hasOwnProperty(name)) {
delete localStorage[name];
}
};
return that;
};
LocalMap.destroy = function() {
for (var item in localStorage) {
if (localStorage.hasOwnProperty(item)) {
delete localStorage[ item ];
}
}
};
LocalMap.exists = function (name) {
return (localStorage[name]) ? true : false;
};
下面是get和set功能单元测试:
test("Test set()", function() {
var map = LocalMap('test-namespace');
///
map.set("var-1", "val-1");
map.set("var-2", "val-2");
map.set("var-3", "val-3");
//
ok(map.isSet("var-1"), "A variable should be successful set.");
ok(map.isSet("var-2"), "A variable should be successful set.");
ok(map.isSet("var-3"), "A variable should be successful set.");
});
test("Test get()", function() {
var map = LocalMap('test-namespace');
map.set("var-1", "val-1");
map.set("var-2", "val-2");
map.set("var-3", "val-3");
///
var var1 = map.get("var-1");
var var2 = map.get("var-2");
var var3 = map.get("var-3");
var var4 = map.get("var-4");
//
equal(var1, "val-1", "A set variable should be succesfully retreived.");
equal(var2, "val-2", "A set variable should be succesfully retreived.");
equal(var3, "val-3", "A set variable should be succesfully retreived.");
equal(var4, null, "A variable that was not set should not be retreived.");
});
希望这会有所帮助,让我知道,如果你有任何问题。
0
下面的代码如何?我复制了from here。其实我喜欢它的代码。
// define dbContext & entities------------------------------------
var DemoDataContext = function() {
nova.data.DbContext.call(this, "Demo", "1.0", "Demo DB", 1000000);
this.users = new nova.data.Repository(this, User, "users");
this.roles = new nova.data.Repository(this, Role, "roles");
};
DemoDataContext.prototype = new nova.data.DbContext();
DemoDataContext.constructor = DemoDataContext;
var User = function() {
nova.data.Entity.call(this);
this.name = "";
this.password = "";
this.birthYear = 1980;
this.createdDate = new Date();
this.deleted = false;
};
User.prototype = new nova.data.Entity();
User.constructor = User;
var Role = function() {
nova.data.Entity.call(this);
this.name = "";
this.createdDate = new Date();
};
Role.prototype = new nova.data.Entity();
Role.constructor = Role;
// end define dbContext & entities------------------------------------
// service methods----------------------------------------------------
function getAllUsers(callback) {
new DemoDataContext().users.toArray(function (users) {
alert(users.length);
callback(users);
});
}
function getUserByName(name, callback) {
new DemoDataContext().users.where("name='" + name + "'").toArray(function (users) {
callback(users.firstOrDefault());
});
}
function addRole(roleName, callback) {
var role = new Role();
role.name = roleName;
var db = new DemoDataContext();
db.roles.add(role);
db.saveChanges(callback);
}
function updateUserPassword(username, password, callback) {
getUserByName(username, function (user) {
if (user == null) {
throw "no user found.";
}
user.password = password;
var db = new DemoDataContext();
db.users.update(user);
db.saveChanges(callback);
});
}
function deleteUserByName(name, callback) {
getUserByName(name, function (user) {
if (user == null) {
throw "no user found.";
}
var db = new DemoDataContext();
db.users.remove(user);
db.saveChanges(callback);
});
}
// end service methods----------------------------------------------------
+0
非常感谢我正在努力。您的帮助表示赞赏! –
相关问题
- 1. PCL保存图像文件到本地文件系统
- 2. JavaScript创建并保存文件到本地文件系统
- 3. Apache Ignite缓存在本地文件系统上保留
- 4. 用html保存在本地系统中的文件
- 5. android:文件保存系统
- 6. 将html文件保存到本地系统使用ASP.NET
- 7. 如何将svg画布保存到本地文件系统
- 8. 将数据从Drupal保存到本地系统文件
- 9. jsdom本地文件系统
- 10. Scrapy - 如何在S3和本地文件系统中保存json文件 - 同时
- 11. 建立在线订单 - 库存系统
- 12. Phonegap文件系统'undefined'
- 13. 本地文件的Phonegap文件存储
- 14. 在本地保存文件
- 15. 无法创建本地文件系统如何写入本地文件系统
- 16. 文件系统API - 从本地驱动器上传到本地文件系统
- 17. 如何将日志/数据保存在docker中的本地文件系统
- 18. php库存和订单管理系统
- 19. 使用Phonegap将文件下载到本地文件系统显示进度条
- 20. 保存文件系统元数据
- 21. 将pdf保存到文件系统
- 22. 无法加载本地存储在系统中的JSON文件
- 23. C锐利的系统IO保存文本文件问题
- 24. 本地文件blob保存
- 25. HDFS vs LFS - Hadoop Dist。文件系统建立在本地文件系统上?
- 26. 如何地块保存到文件系统
- 27. 将prestashop订单保存为文本文件
- 28. 如何将文件从本地文件系统复制到HDFS文件系统?
- 29. TFBuild访问本地文件系统
- 30. Azure Webjob - 访问本地文件系统
非常好。非常感谢!!!让我看看代码,如果我有一些问题,我一定会让你知道。再次感谢您花时间回答这个问题 - 非常感谢! –