0

我对对数据库的异步调用有点困惑。

我只想有一个用于调用 web sql 的 javasctipt 适配器类。但我不太确定如何做到这一点。可能有人对我有一个很好的提示。

函数 OfflneAppDBAdapter.prototype.IsDeviceConfigured() 应该返回 true 或 false,具体取决于 Table DeviceConfig 中是否有任何项目。

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());

正如预期的那样,控制台打印“假”。我知道 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 回答