0

I'm using Jaydata with it's Indexeddbprovider, I've a problem in adding scenario.

When there are multiple adds, just the first one works!

$data.Entity.extend("Person", {
   Id: { type: "int", key: true, computed: false },
   Task: { type: String, required: true, maxLength: 200 },
   DueDate: { type: Date },
   Completed: { type: Boolean },
   University: { type: "int"},
   Degree: { type: "int" }
 });

  $data.EntityContext.extend("ClientDatabase", {
        People: { type: $data.EntitySet, elementType: Person }
  });

  var db = new ClientDatabase({
        provider: 'indexedDb', databaseName: 'ClientDB', version: 1
  });

var newEntity = {
    Id: 1,
    Task: 'task1',
    DueDate: new Date(),
    Completed: false,
    University: 1,
    Degree: 1
};

 var newEntity2 = {
    Id: 4,
    Task: 'task4',
    DueDate: new Date(),
    Completed: false,
    University: 4
    Degree: 4
};

add(db, newEntity, entity1AddedSuccessfully);

function entity1AddedSuccessfully(){
  add(db, newEntity2);
}

function add(db, entity, callback){
    db.onReady({
        success: function () {
            db["_People"].add(entity);
            db.saveChanges(function () {
                if (callback !== undefined) {
                    callback(entity);
                }
            });
        }
    });
 }

The problem is in this scenario, newEntity is just added to ClientDB and there is no newEntity2!

Any help would be appreciated.

Mohsen Asfia
  • 235
  • 1
  • 3
  • 12

3 Answers3

3

I made many changes in the code, you can check it out on JSFiddle.

Important things:

  • use the typed db.People collection to query and insert the records (not db["_People"])
  • use typed entites - new Person()
  • if you use auto-generated Id, it's better to not set it manually :)
  • after I modified all of these above, your logic passed the same newEntity to the callback, so I had 3 records instead of 2. I simplified the code by removing the callback function definitions

Check out the JSFiddle code and share your feedback if you wanted to achieve this behavior

   $data.Entity.extend("Person", {
   Id: { type: "int", key: true, computed: true },
   Task: { type: String, required: true, maxLength: 200 },
   DueDate: { type: Date },
   Completed: { type: Boolean },
   University: { type: "int"},
   Degree: { type: "int" }
 });

$data.EntityContext.extend("ClientDatabase", {
    People: { type: $data.EntitySet, elementType: Person }
});

var db = new ClientDatabase({
    provider: 'indexedDb', databaseName: 'ClientDB', version: 1
});

db.onReady(function(){
    var newEntity = new Person( {
        Task: 'task1',
        DueDate: new Date(),
        Completed: false,
        University: 1,
        Degree: 1
    });

     var newEntity2 = new Person({
        Task: 'task4',
        DueDate: new Date(),
        Completed: false,
        University: 4,
        Degree: 4
    });

    db.People.add(newEntity);
    db.saveChanges(function() {
        db.People.add(newEntity2);
        db.saveChanges(function() {alert(newEntity2.Id);});
    });

});
Robesz
  • 1,646
  • 11
  • 13
  • Thanks for your detailed answer, The problem is when computed is equals false and I want to do the add/save operations separately not to do second one in save callback of the first one, you can test it right now it wouldn't add second record, I really need to do the multiple operations separately! – Mohsen Asfia Feb 21 '13 at 13:47
  • Did you succeed reproduce my problem? – Mohsen Asfia Feb 21 '13 at 14:39
  • This is because the saving of the first one is still happening. This is why you have to call the second add in the saveChanges completion handler. You can not use the same db until the save is completed (it mixes db state). To do it separately (independently) create two db instances. – Peter Aron Zentai Feb 21 '13 at 16:19
  • Robesz is updating the fiddle so that is contains manual id fields instead of automatic increments - but the solution is quite the same. – Peter Aron Zentai Feb 21 '13 at 16:20
  • I wish there was a better solution than creating multiple db instances! tell me please if there's any other solution ... – Mohsen Asfia Feb 21 '13 at 20:09
  • I also checked updated version of fiddle by Robesz (Thanks @Robesz), I've already mentioned in my first comment that this solution is not what we are looking for because we want to do the add operation separately and in independent manner. – Mohsen Asfia Feb 21 '13 at 20:16
  • Every context keeps track of what the next saveChanges should do, so when you add/attach/detach/remove an entity the context registers this and when you actually call saveChanges() then the entities are persisted. Once the persistance is finished, the context is cleared and you can use it again. If you try to add an entity during saveChanges() then you will not succeed. If you have independent contexes then you will not run into this problem. – Gabor Dolla Feb 21 '13 at 21:03
  • one solution could be to use our ItemStore API instead of JSQL (under the hood ItemStore API will create a new context for you), so your code could be: $data('Person').save({ Task : 'task4' ... }); and you can issue a save at any time and any part of your code – Gabor Dolla Feb 21 '13 at 21:10
  • also, we will announce a new feature soon with which you can also achieve what you want and much much more – Gabor Dolla Feb 21 '13 at 21:13
  • Gabor I'm really thankful for your answers, I think right now we should choose one of the solutions you advised, I'm also looking forward the new features :) – Mohsen Asfia Feb 22 '13 at 15:05
0

I'm afraid that you've mixed it up a little bit. $data.Entity and $data.EntityContext are for model definition

var db = new ClientDatabase(...)

db is a database context and it has no EntityContext method or property

so your code should look like:

db.onReady()

db.People.add(entity)

db.saveChanges(...)
Gabor Dolla
  • 2,680
  • 4
  • 14
  • 13
  • yeah you're right db does not have any EntityContext, it was my fault in typing the example, because we wrap our object into EntityContext Property of our db object! so I edited my sample, but my problem is just the first add operation is working!! – Mohsen Asfia Feb 21 '13 at 12:28
  • Hi Mohsen! You can find more code examples here: http://jaydata.org/blog/understand_Jaydata_in_seven_simple_steps – Robesz Feb 21 '13 at 12:31
  • Hi Robesz, I saw your sample before, the problem appears when I want to specify the Id property of each new object by my self also I want to have multiple add/save, in this scenario just the first operation is working!! the other add/save operations got failed!! – Mohsen Asfia Feb 21 '13 at 12:57
-1

I hope this helps someone… it did the trick for me… http://jaydata.org/forum/viewtopic.php?f=3&t=184