0

データベースへの非同期呼び出しについて少し混乱しています。

Web SQLへの呼び出し用のjavasctiptアダプタークラスが必要です。しかし、これを行う方法がよくわかりません。おそらく誰かが私に良いヒントを持っています。

関数 OfflneAppDBAdapter.prototype.IsDeviceConfigured() は、テーブル DeviceConfig にアイテムがあるかどうかに応じて、true または false を返す必要があります。

function OfflneAppDBAdapter() {
self = this;
this.deviceIsConfigured = false;

this.Init = function () {

    $data.Entity.extend("$de.offlineapp.DeviceConfig", {
        Id: { type: "int", key: true, computed: true },
        Name: { type: "string", required: true },
        Token: { type: "string" },
        Type: { type: "string" }
    });


    $data.EntityContext.extend("$de.offlineapp.DataContext", {
        DeviceConfig: { type: $data.EntitySet, elementType: $de.offlineapp.DeviceConfig }
    });

}
self.Init();

$de.offlineapp.context = new $de.offlineapp.DataContext({
    name: "webSql", databaseName: "OfflineApp"
});

$de.offlineapp.context.onReady(function () {

});

}

// ************************************************************************ 
// PUBLIC METHODS -- ANYONE MAY READ/WRITE 
// ************************************************************************ 
OfflneAppDBAdapter.prototype.AddDeviceConfig = function (deviceName, deviceToken, deviceTyp) {
$de.offlineapp.context.onReady(function () {
    var promise = $de.offlineapp.context.DeviceConfig.toArray(function (x) {
        if (x.length == 0) {
            var emp = new $de.offlineapp.DeviceConfig({ Name: deviceName, Token: deviceToken, Type: deviceTyp });
            $de.offlineapp.context.DeviceConfig.add(emp);
            $de.offlineapp.context.saveChanges();
        }
    }
)
});

}

OfflneAppDBAdapter.prototype.IsDeviceConfigured = function () {

$de.offlineapp.context.onReady(function () {
    var promise = $de.offlineapp.context.DeviceConfig.toArray(function (x) {
        if (x.length == 0) {
            this.deviceIsConfigured = true;
        }
    }
)
});

return this.deviceIsConfigured;
} 


var myOfflineAppDBAdapter = new OfflneAppDBAdapter();
myOfflineAppDBAdapter.AddDeviceConfig("DeviceName", "Token", "iPad");
console.log(myOfflineAppDBAdapter.IsDeviceConfigured());

予想どおり、コンソールには「false」が出力されます。jaydata 呼び出しはコールバックで機能し、コールバックはメイン クラスの一部ではないことを認識しています。しかし、そうする可能性があるに違いありませんか?

どんな助けでも本当に感謝します。

よろしくお願いします.... クリス

更新:スタートアップコードをリクエストしたとおり:

function OfflineApplication()
    {
        self = this;
    }


    OfflineApplication.prototype.StartApplication = function () {

        //Check if online, then sync and 
        if (navigator && navigator.onLine === true) {
            this.IsDeviceConfigured();
        }
        else {

        }


    }

    ///check if the device has a base configuration
    OfflineApplication.prototype.IsDeviceConfigured = function () {

        myOfflineAppDBAdapter.GetDeviceConfiguration(function (result) {
            if (result.length > 0) {
                myOfflineAppDBAdapter.deviceIsConfigured = true;
                myOfflineApplication.HasDeviceAnApplication();
            }
            else {
                ///Get the device base conf from the server.
                myOfflineAppSynchronisationAdapter.getDeviceConfigurationByToken(token, myOfflineApplication.HasDeviceAnApplication);
                myOfflineAppDBAdapter.deviceIsConfigured = true;
            }
        });
    }
    ///check if the device has an "application config" in general 
    OfflineApplication.prototype.HasDeviceAnApplication = function () {
        myOfflineAppDBAdapter.GetDeviceAnApplication(function (result) {
            if (result.length > 0) {
                myOfflineApplication.IsDeviceApplicationVersionLatest(result);
            }
            else {
                myOfflineApplication.GetApplication(false);
            }
        });
    }

    ///the application config could differ from time to time, so we have to check if a different application should be synct with the device
    OfflineApplication.prototype.IsDeviceApplicationVersionLatest = function (result) {

        myOfflineAppDBAdapter.DeleteDeviceAnApplication(function () {  });
        console.log(result);
    }


    ///get the application from the server
    OfflineApplication.prototype.GetApplication = function (clearConfig) {
        if (clearConfig === true)
        {


        }

        myOfflineAppSynchronisationAdapter.getDeviceApplicationByToken(token, myOfflineApplication.LoadApplication);

    }


    OfflineApplication.prototype.LoadApplication = function () {
        console.log('Now everything is finde and the application gets loaded..');
    }



    var myOfflineAppDBAdapter = new OfflneAppDBAdapter();
    var myOfflineAppSynchronisationAdapter = new OfflineAppSynchronisationAdapter();
    var myOfflineApplication = new OfflineApplication();

    myOfflineApplication.StartApplication();
4

1 に答える 1

1

同期方法はありません。あなたは約束を間違って扱っています。コードをシンプルにしてください :) 次のようなものが必要です。

$data.Entity.extend("$de.offlineapp.DeviceConfig", {
    Id: { type: "int", key: true, computed: true },
    Name: { type: "string", required: true },
    Token: { type: "string" },
    Type: { type: "string" }
});


$data.EntityContext.extend("$de.offlineapp.DataContext", {
    DeviceConfig: { type: $data.EntitySet, elementType: $de.offlineapp.DeviceConfig }
});

var context = new $de.offlineapp.DataContext({
    name: "webSql", databaseName: "OfflineApp"
});

function AddDeviceConfig(deviceName, deviceToken, deviceTyp) {
    return context.DeviceConfig.toArray()
    .then(function (x) {
        if (x.length == 0) {
            var emp = new $de.offlineapp.DeviceConfig({ Name: deviceName, Token: deviceToken, Type: deviceTyp });
            context.DeviceConfig.add(emp);
            return context.saveChanges();
        }
    })
}

function IsDeviceConfigured() {
    return context.DeviceConfig.toArray()
    .then(function (x) {
        return x.length > 0;
    })
}

context.onReady()
.then(IsDeviceConfigured)
.then(console.log)
.then(function() { return AddDeviceConfig("DeviceName", "Token", "iPad"); })
.then(IsDeviceConfigured)
.then(console.log);

これを行うフィドルは次のとおりです。http://jsfiddle.net/JayData/cpT5q/1/

于 2013-08-21T19:07:45.097 に答える