gpt4 book ai didi

javascript - 使用 JayData 进行基本数据存储

转载 作者:行者123 更新时间:2023-11-30 18:07:45 28 4
gpt4 key购买 nike

我尝试实现我的第一个基于 JayData 的应用程序。我需要我的 HTML5/JavaScript 应用程序在客户端存储复杂的数据(主要是一对多关系)。

我的模型看起来像这样(抱歉,如果名称不是很明确):

enter image description here

我尝试用 JayData 翻译它,如果我错了请纠正我:

$data.Entity.extend("Test", {
Id: { type: "int", key: true, computed: true },
Name: { type: "string", required: true, maxLength: 200 },
Chapters: { type: Array, elementType: "Chapter", inverseProperty: "Test" }
});

$data.Entity.extend("Chapter", {
Id: { type: "int", key: true, computed: true },
Name: { type: String, required: true, maxLength: 200 },
Test: {type: "Test", inverseProperty: "Chapters" },
Checks: { type: Array, elementType: "Check", inverseProperty: "Chapter" }
});

$data.Entity.extend("Check", {
Id: { type: "int", key: true, computed: true },
Name: { type: String, required: true, maxLength: 200 },
Status: { type: "int", required: true },
Chapter: { type: Chapter, inverseProperty: "Checks" }
});

$data.EntityContext.extend("CheckDb", {
Tests: { type: $data.EntitySet, elementType: Test },
Chapters: { type: $data.EntitySet, elementType: Chapter },
Checks: { type: $data.EntitySet, elementType: Check }
});

编辑

如何填充所有表格?首先,我试过:

var myDB = new CheckDb({ 
name: 'local', databaseName: 'CheckDatabase'
});

myDB.onReady(function() {
myDB.Tests.add({
Name: "Test 1"
});
myDB.saveChanges();
});

但是,如果我想将ChaptersChecks 添加到我的Test 中怎么办?

谢谢

最佳答案

好吧,我终于自己找到了:

myDB.onReady(function() {
var check = new Check({Name: "Check 1", Status: 1});
var chapter = new Chapter({Name: "Chapter 1"});
chapter.Checks = new Array();
chapter.Checks.push(check);

var myTest = myDB.Tests.add({
Name: "Test 1",
Chapters: [chapter]
});
myDB.saveChanges();
});

没那么难:)

关于javascript - 使用 JayData 进行基本数据存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15388657/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com