0

此代码应在您的浏览器中按原样运行。

<!DOCTYPE html>
<html>
    <head>
        <title>JayData testing</title>

        <script type="text/javascript" src="jquery-1.9.1.js"></script>
        <script type="text/javascript" src="jaydata.js"></script>
        <script type="text/javascript" src="SqLiteProvider.js"></script>

        <script type="text/javascript">

            $data.Entity.extend('$data.Types.ReferencedEntity', {
                Oid: {type: $data.Integer, key: true, computed: true},
                Codigo: {type: $data.String},
                Nombre: {type: $data.String},
                MainsByOne: {type: $data.Array, elementType: '$data.Types.MainEntity', inverseProperty: 'ReferenceOne'},
                MainsByTwo: {type: $data.Array, elementType: '$data.Types.MainEntity', inverseProperty: 'ReferenceTwo'}
            });

            $data.Entity.extend('$data.Types.MainEntity', {
                Oid: {type: $data.Integer, key: true, computed: true},
                Codigo: {type: $data.String},
                ReferenceOne: {type: '$data.Types.ReferencedEntity', inverseProperty: 'MainsByOne'},
                ReferenceTwo: {type: '$data.Types.ReferencedEntity', inverseProperty: 'MainsByTwo'}
            });

            $data.EntityContext.extend('$data.Types.LocalDBContext', {
                ReferencedEntities: {type: $data.EntitySet, elementType: $data.Types.ReferencedEntity},
                MainEntities: {type: $data.EntitySet, elementType: $data.Types.MainEntity}
            });

            window.app = {};

            app.localdb = new $data.Types.LocalDBContext({
                name: 'webSql',
                databaseName: 'Test',
                version: "1.0",
                maxSize: 5 * 1024 * 1024,
                dbCreation: $data.storageProviders.DbCreationType.DropTableIfChanged
            });

            app.localdb.onReady(function() {
                console.log("smarterpjs.localdb.onReady!");
            });

            app.run = function() {
                /* Create the entities */
                var a = new $data.Types.ReferencedEntity();
                app.localdb.ReferencedEntities.add(a);
                a.Codigo = 'UNI';
                a.Nombre = 'UNIDAD';

                var m = new $data.Types.MainEntity();
                app.localdb.MainEntities.add(m);
                m.Codigo = 'MAIN';
                m.ReferenceOne = a;
                m.ReferenceTwo = a;

                app.localdb.saveChanges();

                /* Now query for editing... */

                var query = app.localdb.MainEntities
                        .include('ReferenceOne')
                        .include('ReferenceTwo')
                        .filter(function(m) {
                            return m.Codigo === 'MAIN';
                        }, undefined)
                        .first();

                query.then(function(result) {
                    /* As i need to 'edit' the result
                     * then i must attach the MainEntity itself,
                     * and the referenced objects ReferenceOne, and ReferenceTwo
                     */
                    app.localdb.attach(result);
                    app.localdb.attach(result.ReferenceOne);

                    /* The problem comes here, 
                     * as the entity Codigo 'MAIN' references on both fields 'ReferenceOne' and 'ReferenceTwo'
                     * the same 'ReferencedEntity'
                     */
                    app.localdb.attach(result.ReferenceTwo);
                });
            }

        </script>
    </head>
    <body>
        <div onclick="app.run()">CLICK ME!</div>
    </body>
</html>

拥有 jaydata 的测试应用程序,我的问题嵌入在相同的代码中。但我试图解释更多:我有一个 MainEntity 表,其中 2 个字段指向另一个 ReferencedEntity 表。例如:

ReferencedEntities = [ { Codigo: 'UNI' } ]
MainEntities = [ { 
 Codigo: 'MAIN', 
 ReferenceOne: { Codigo: 'UNI' } , 
 ReferenceTwo: { Codigo: 'UNI' }
}

有了这个非常简单的数据,当我尝试只加载一个 MainEntity 以使其完全可用,以便用户可以编辑他们的任何字段时,我会收到以下错误:

**Context already contains this entity!!!** 

希望我明白我的意思,并且代码运行显示当前 v1.3.2 的 jaydata 的问题。

编辑:

我找到了使用简单代码获取完全加载对象及其引用的“方法”。

更改在query.then()上,如下:

query.then(function(result) {
  app.localdb.attach(result);
  result.ReferenceOne = app.localdb.attachOrGet(result.ReferenceOne);
  result.ReferenceTwo = app.localdb.attachOrGet(result.ReferenceTwo);
});

那样:

  • 我总是使用 attachOrGet 加载每个引用的实体,以防它已经在上下文中
  • 我将每个返回的对象分配给原始对象,因此最终的“结果”作为编辑该实例、保存和保存更改的所有内容

希望有人觉得这很有用!

4

1 回答 1

0

I do not think that it will work. You can try to use attachOrGet when you attach the second child..

于 2013-10-31T20:13:21.547 回答