繁体   English   中英

如何使用Jaydata进行同步调用

[英]How to do a synchronous call with jaydata

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

我只想有一个javasctipt适配器类来调用Web sql。 但是我不太确定该怎么做。 可能有人对我有很好的提示。

函数OfflneAppDBAdapter.prototype.IsDeviceConfigured()应返回true或false,这取决于表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());

如预期的那样,控制台将输出“ 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();

没有同步方式。 您处理的诺言是错误的。 使您的代码简单:)您将需要以下内容:

$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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM