mardi 17 juin 2014

MVVM: Isolated session and multiple creating phantom records.

In this example http://ift.tt/1vzucAV (source: http://ift.tt/1oAavZR ) that is designed with mvvm

you can add new device by clicking new device.
new device is created in child session to prevent record existance if user cancels creating.

this is main view controller handler that shows new window (view) with new session:



Code:



var win = Ext.widget('physicaldevice', {
session: true,
modal: true
});
console.log("win.getSession().getParent(), this.getSession():", win.getSession().getParent()== this.getSession());
//put viewmodel into parent viewmodel
win.getViewModel().setParent(this.getViewModel());
//create new record, etc...
win.getController().beginCreate();
//show window
win.show();

this is window's view controller:


Code:



beginCreate: function () {
var record = this.getSession().createRecord('PhysicalDevice', {});
console.log("record.get('id'):", record.get('id'));
record.set('station_id', 1);
this.getViewModel().linkTo('currentItem', record);
},

this is window's view controller action to save session (user clicks OK)


Code:



endCreate: function () {
this.getSession().save();
this.getViewModel().get('physicalDevices').add(this.getViewModel().get('currentItem'));
this.getView().close();
}

I also setup model client id generation strategy.


Code:



idProperty: 'id', // this is the default value (for clarity)
clientIdProperty: 'clientId',
identifier: 'negative', // to generate -1, -2 etc on the client

But the problem is that I get exception when clicking "OK" on newly created device, for the second time.

Excepted scenario (open debug tools to see exceptions):



  1. click 'add device' (child session willl be spawned)

  2. enter name into form (i.e. example1)

  3. click ok (child session will be saved)

  4. one row is added: {id:-1, nazwa:'device1'}

  5. click 'add device' (child session willl be spawned)

  6. enter name into form (i.e. example2)

  7. click ok (child session will be saved)

  8. another row is added: {id:-2, nazwa:'device2'}


But now real scenario is:

  1. click 'add device' (child session willl be spawned)

  2. enter name into form (i.e. example1)

  3. click ok (child session will be saved)

  4. one row is added: {id:-1, nazwa:'device1'}

  5. click 'add device' (child session willl be spawned)

  6. enter name into form (i.e. example2)

  7. click ok (child session will be saved)

  8. exception occured: [E] Ext.data.Session.onInvalidEntityCreate(): Cannot create, record already not exists: -1@PhysicalDevice


So, what I am doing wrong? Everything works ok if we have one session (without creating child session and saving it) - new records are added with id: -1, -2, -3...

It is a bug?


Aucun commentaire:

Enregistrer un commentaire