Dynamically created context

by JayData

HTML

<script src="http://jaydata-cdn.s3.amazonaws.com/datajs-1.0.3-patched.js"></script>
<script src="http://include.jaydata.org/jaydata.js"></script>

JavaScript

var odataServiceUrl = 'http://jaydata.org/examples/Northwind.svc';

//create an empty context type
$data.EntityContext.extend('NorthwindModel.Context', {

});

//define an entity
$data.Entity.extend('NorthwindModel.Product', {
     'Product_ID': { 'key':true, 'type':'Edm.Int32', 'nullable':false, 'computed':true },
     'Supplier_ID': { 'type':'Edm.Int32', 'nullable':true },
     'Category_ID': { 'type':'Edm.Int32', 'nullable':true },
     'Product_Name': { 'type':'Edm.String', 'nullable':false, 'required':true, 'maxLength':40 },
     'English_Name': { 'type':'Edm.String', 'nullable':true, 'maxLength':40 },
     'Quantity_Per_Unit': { 'type':'Edm.String', 'nullable':true, 'maxLength':20 },
     'Unit_Price': { 'type':'Edm.Decimal', 'nullable':true },
     'Units_In_Stock': { 'type':'Edm.Int16', 'nullable':true },
     'Units_On_Order': { 'type':'Edm.Int16', 'nullable':true },
     'Reorder_Level': { 'type':'Edm.Int16', 'nullable':true },
     'Discontinued': { 'type':'Edm.Boolean', 'nullable':false, 'required':true },
     'Category': { 'type':'NorthwindModel.Category', 'inverseProperty':'Products' }     
  });
//add member (EntitySet definition) dynamically to empty context
// this step NOT recommended after context creation
NorthwindModel.Context.addMember('Products', { type: $data.EntitySet, elementType: NorthwindModel.Product });

//an other entity
$data.Entity.extend('NorthwindModel.Category', {
     'Category_ID': { 'key':true, 'type':'Edm.Int32', 'nullable':false, 'computed':true },
     'Category_Name': { 'type':'Edm.String', 'nullable':false, 'required':true, 'maxLength':15 },
     'Description': { 'type':'Edm.String', 'nullable':true, 'maxLength':Number.POSITIVE_INFINITY },
     'Picture': { 'type':'Edm.Binary', 'nullable':true, 'maxLength':Number.POSITIVE_INFINITY },
     'Products': { 'type':'Array', 'elementType':'NorthwindModel.Product', 'inverseProperty':'Category' }
  });
//an other EntitySet on Context type
// this step NOT recommended after...