2013-01-12 86 views
0

嗨,我正在使用phonegap开发购物应用程序。我想给用户一个选择,以保存他们的订单,并完成他/她找到方便的时间。我的问题在哪里保存订单数据。移动设备的本地文件系统或本地数据库?我希望将json格式的订单 保存在本地文件中。请为我提供最好的选择。也是一个片段将高度赞赏。谢谢phonegap在本地文件系统保存订单

回答

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

非常好。非常感谢!!!让我看看代码,如果我有一些问题,我一定会让你知道。再次感谢您花时间回答这个问题 - 非常感谢! –

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

非常感谢我正在努力。您的帮助表示赞赏! –