我尝试实现我的第一个基于JayData的应用程序。我需要HTML5 / JavaScript应用程序来存储复杂的客户端数据(主要是一对多关系)。
我的模型如下所示(抱歉,如果名称不是很明确的话):
我尝试用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();
});
但是,如果我想在
Chapters
中添加Checks
和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/