我尝试实现我的第一个基于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
该怎么办?
由于
答案 0 :(得分:2)
好的,我终于找到了它自己:
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();
});
这并不难:)
答案 1 :(得分:0)
您的代码没问题。我已经用你的代码创建了一个jsfiddle,它可以在不改变单个字符的情况下工作。 包括jQuery 1.8+和然后 http://include.jaydata.org/jaydata.js