0

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

4

2 回答 2

1

您还可以使用HTML5 localStorage作为文件存储的更简单替代方案。我一直在使用 localStorage 的封装版本来促进 get/set 操作并减少命名空间污染。请参阅下面的代码库:

/**
 * 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.");
});

希望这会有所帮助,如果您有任何问题,请告诉我。

于 2013-01-12T01:36:27.077 回答
0

How about the below code? I copied it from here. Actually I like its code.

// 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---------------------------------------------------- 
于 2013-01-12T15:11:53.457 回答