2013-01-12 88 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

非常感謝我正在努力。您的幫助表示讚賞! –