using addProperty() to set fields manually
This can be used to implement 1:1 relationships
by JayData
HTML
<script src="http://include.jaydata.org/jaydata.min.js"></script>
JavaScript
$data.Entity.extend("$example.types.Car", {
Id: { type: "int", key: true, computed: true },
Color: { type: "string" },
WheelFLId: { type: "int" }
});
//add new property to the entity definition
$example.types.Car.addProperty("WheelFL", function(){
console.log("Wheel id:"+this.WheelFLId);
return this.__WheelFL;
});
//additional option to pre-load all fields
$example.types.Car.addProperty("initCustomFields", function(){
//load all wheels from id
var that = this;
return ctx.Wheels.single("it.Id == this.id",{id : this.WheelFLId},function(data){
that.__WheelFL = data;
});
// other fields to set...
});
$data.Entity.extend("$example.types.Wheel", {
Id: { type: "int", key: true, computed: true },
Size: { type: "string" }
});
$data.EntityContext.extend('$example.types.TestContext', {
Cars: { type: $data.EntitySet, elementType: $example.types.Car},
Wheels: { type: $data.EntitySet, elementType: $example.types.Wheel }
});
var ctx = new $example.types.TestContext({name: 'sqLite', databaseName: 'test5' });
ctx.onReady(function(){
console.log(ctx._storageModel[0]);
ctx.Wheels.add(new $example.types.Wheel({Size:'14'}));
ctx.Wheels.add(new $example.types.Wheel({Size:'15'}));
ctx.Wheels.add(new $example.types.Wheel({Size:'17'}));
ctx.Cars.add(new $example.types.Car({Color:'red', WheelFLId:3}));
ctx.saveChanges(function(){
console.log("Start solution");
ctx.Cars.single("it.Id==1",null,function(car){
car.initCustomFields.then(function(){ //this isn't necessary if you don't want to set many fields at the beginning
console.log(car.WheelFL);
});
});
});
});