4

I'm trying to write a simple CRUD functionality in Jaydata, I had written this simple code for update operation:

SampleClass.prototype.Load = function(input1,callback)
{
  var param='it.Name=="'+input1+'"';
  this.data.items.filter(param).forEach(function(ii)
    {
      callback(ii);
    });
  this.data.items.saveChanges();
};

so when I call:

t.Load('Entry4',function(res){console.log(res.Name)})

It works like a charm! But If I call an update operation for callback like:

t.Load('Entry4',function(res){res.Name="Entry5"})

It doesn't change anything in the DB. I have seen something like beginTransaction function as in http://jaydata.org/examples/JayDataPro/ToDoList_complex, but I couldn't understand the essence of it.

2 Answers2

3

Special thanks to Gabor Dolla

In order to update a value in JayData:

  • DB has to have a primary-key inside it.
  • Change non-key attributes
  • Call the asynchronous save() function after it.

solution to the question is: after changing the object field's definition like this:

Name{ type:'string', **key:true**}

You can query on anything but only change non-key attributes of them

t.Load('Entry4',function(res){res.LastName="Entry5";res.save()});
1

I think only the attach() is missing before modifing the entity.

this.data.items.attach(res);

BTW, I would move the saveChanges to the update callback, because you don't need it in readonly scenario.

Robesz
  • 1,646
  • 11
  • 13
  • Please clarify: you cannot use this and res, they are not defined together. I used t.data.items.attach(res); both before and after changing the result, I also tried to create a temporary variable and attach it too. None works. – AutomaticHourglass May 03 '13 at 06:33
  • 2
    Hi! 'this' is a tricky thing, you don't really need it so you'd better avoid it. As Robesz told you, you need attach before you start changing properties so JayData can track the changes. An alternative solution is to use our other api, so change the second callback function to: t.Load('Entry4',function(res){res.Name="Entry5";res.save();}) – Gabor Dolla May 03 '13 at 07:08