tx test

tx test

by JayData

HTML

<script src="http://include.jaydata.org/pro/jaydata.min.js"></script>

JavaScript

$data.Entity.extend('$mwDemo.TYPE1', {
    ID: { type: 'int', key: true, computed: true},
    FIELD_1: { type: 'string', maxLength: 10 },
    FIELD_2: { type: 'string', maxLength: 10 },
    FIELD_3: { type: 'string', maxLength: 10 }
});
$data.EntityContext.extend('$mwDemo.Context', {
    TABLE1: { type: $data.EntitySet, elementType: $mwDemo.TYPE1 }
});

var ctx = new $mwDemo.Context({ name: 'local', databaseName: 'jayDataEdit3' });

function insert(i){
    var addObject = new newRecord(i,i,i);
    return ctx.beginTransaction(true, undefined)
    .then(function(tran) {
        console.log(i);
        return addRecord(ctx,tran,addObject);
    })
    .fail(function(reason) {
        console.log(reason);
    });
}

function addRecord(ctx,tran,addObject) {
    console.log('addRecord');
    return ctx.TABLE1
	    .filter(function(t){
		    return t.FIELD_1 == this.FIELD_1 && t.FIELD_2 == this.FIELD_2;
        }, addObject)
        .toArray(undefined, tran)
        .then(function(result, tran) {
                var entity = new $mwDemo.TYPE1(addObject);
                if (!result.length) {
                    console.log('add Entry');
                    ctx.TABLE1.add(entity);
                }
                else {
                    console.log('edit Entry');
                    var editEntity = result[0];
                    ctx.TABLE1.attach(editEntity)
                    editEntity.FIELD_3 = entity.FIELD_3;
                }
                console.log('save');
                return ctx.saveChanges(undefined, tran);
        })
}

function newRecord(FIELD1,FIELD2,FIELD3){
    this.FIELD_1 = FIELD1;
    this.FIELD_2 = FIELD2;
    this.FIELD_3 = FIELD3;
}

ctx.onReady( function() {
  var promises = [];
  for (i=0; i<100; i++) {
      promises.push(insert(i.toString()));
      promises.push(insert(i.toString()));
      promises.push(insert(i.toString()));
  }
  $.when.apply(this, promises)
  .then(function() {
      console.log('finished');
  })
 ...