0

こんにちは、phonegap を使用してショッピング アプリを開発しています。私は、ユーザーが注文を保存して、便利だと思ったときに完了するオプションをユーザーに提供したいと考えています。私の質問は、注文データをどこに保存すればよいですか。モバイルデバイスのローカルファイルシステムまたはローカルデータベース? 注文をjson形式でローカルファイルに保存したいと思います。私に最適なオプションを提案してください。また、スニペットも高く評価されます。ありがとう

4

2 に答える 2

1

ファイル ストレージのより簡単な代替手段として、HTML5 localStorageを使用することもできます。get/set 操作を容易にし、名前空間の汚染を減らすために、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.");
});

ご不明な点がございましたら、お気軽にお問い合わせください。

于 2013-01-12T01:36:27.077 に答える
0

以下のコードはどうですか?ここ からコピーしました。実際、私はそのコードが好きです。

// 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 に答える