Add field to model dynamically

by JayData

HTML

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://include.jaydata.org/jaydata.min.js"></script>

JavaScript

$data.Entity.extend('Product', {
  id: { type: 'int', key: true, computed: true },
  Name: { type: 'string' }
});

$data.EntityContext.extend('Northwind', {
  Products: { type: $data.EntitySet, elementType: Product},
});

Product.addMember('Description', {
    type:'string', 
    key: false, 
    computed: false, 
    required: false
});

var context = new Northwind({provider: 'webSql', databaseName: 'Northwind'});

context.onReady(function() {
    var product1 = new Product({ Name: 'Beer', Description: 'tasty'});
    context.Products.add(product1);
    context.saveChanges(function(result) {
        //check the content of WebSQL DB
        console.log(product1);
    });
});