Basic data storage with JayData

懵懂的女人 提交于 2019-12-11 12:37:59

问题


I try to implement my first JayData based application. I need my HTML5/JavaScript app to store complex data client-side (mostly one-to-many relations).

My model looks like this (sorry if the names are not very explicit):

I've tried to translate it with JayData, correct me if I'm wrong:

$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 }
});

EDIT

How to fill all tables? To start, I've tried:

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

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

But what if I want to add Chapters and Checks to my Test?

Thanks


回答1:


Okay I've finally found it by myself:

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();
});

It wasn't that hard :)




回答2:


Your code is ok. I've created a jsfiddle with your code and it works without changing a single character. include jQuery 1.8+ and then http://include.jaydata.org/jaydata.js



来源:https://stackoverflow.com/questions/15388657/basic-data-storage-with-jaydata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!