ExtJS 4 - Update / Update Single Entry

I have a problem that listens to me.

I have a grid, and when I dblclick on an element, I want to open a window for editing this element. Pretty standard stuff. The problem is that I want to be sure that the record is updated, because other people using the program may have changed it or even deleted it.

I could reload the repository, but I want only one specific record to be checked ... So I decided that I just go, get the data, create another record and replace the existing one in the store, but I really want to know the best way to do this

Remember that the RESTful proxy server is not an option for me, although I do not know if the update operation works in this case (server β†’ client).

EDIT: this may help someone: all I did was copy the data and raw objects from the new record to the old one, and then β€œcommit” the changes. worked for me.

Thanks.

+8
javascript extjs extjs4 extjs-mvc
source share
4 answers

The best way to do something like this is to reload the record in the event that will open the window. Therefore, where, for example, you upload a record from the grid store to the form inside the window, you can use your model to load from the identifier.

Item.load(id, { success: function(r) { form.loadRecord(r); } }); 

After saving, you should probably also invoke the update in the form of a grid, which will redraw the changes from the save event. You can also use refreshNode ( see the documentation for viewing the grid ) on the exact store entry if you are concerned about performance.

Of course, you do not need to use a simple proxy with this, you can use any proxy server if it will load one record.

+4
source share

ExtJS 4.1

I had a similar problem, and as an experiment tried

sStore.load({ id: mskey, addRecords: true });

where mskey is the uuid of the currently loaded record.

I did not delete the first record (as an experiment) and updated the existing record with the same identifier with new data from the server (through the model β†’ REST proxy). Fine!

I know that you said that you are not using the REST proxy server, but it can help others who found this message to search for search queries such as your topic name (this is how I got here!)

So it looks like addRecords means adding or updating.

FYI, Murray

+5
source share

With ExtJS 4.1, here is an override:

In CoffeeScript:

 # Adds "reload" to models Ext.define "Ext.ux.data.Model", override: "Ext.data.Model", # callBack is called with success:boolean reload: (callBack) -> Ext.getClass(@).load @getId(), success : (r, o) => for k, v of r.data @data[k] = v @commit() callBack(true) if Ext.isFunction(callBack) failure: => callBack(false) if Ext.isFunction(callBack) 

In JS (not tested):

 Ext.define("Ext.ux.data.Model", { override: "Ext.data.Model", reload: function(callBack) { var me = this; return Ext.getClass(this).load(this.getId(), { success: function(r, o) { var k; for (k in r.data) { me.data[k] = r.data[k]; } me.commit(); if (Ext.isFunction(callBack)) { callBack(true); } }, failure: function() { if (Ext.isFunction(callBack)) { callBack(false); } } }); } }); 
+1
source share

I created an override on Ext.data.Model to add an additional method that can be used to update the data of an existing record (model instance).

 Ext.define('Ext.overrides.data.Model', { override: 'Ext.data.Model', /** * Refresh the data of a record from the server */ reloadData: function(cb) { var me = this; var id = me.getId(); Ext.ModelManager.getModel(me.modelName).load(id, { callback: function(record, operation, success) { if (!success) { Ext.Error.raise('Problem reloading data from server in record'); } if (!record) { Ext.Error.raise('No record from server to reload data from'); } //change the data of the record without triggering anything Ext.apply(me.data, record.getData()); //call a final callback if it was supplied if (cb) { cb.apply(me, arguments); } } }); return me; } }); 

Here is how you can use it. This is actually quite simple:

 myRecord.reloadData(function(record, operation, success) { //Done updating the data in myRecord }); 

Inside, it uses the load method for the linked model to create a new record. This new record is based on the same identifier as the original record on which the reloadData method was called. In the callback, the data of the new record is applied to the data of the original record. There were no events that you probably need.

This is Ext 4.2.1. There are probably a dozen scenarios that this solution breaks, but we can always clarify if we can.

Update. This solution basically implements the same as the @Drasill application. Well, well ... This test has been tested.

0
source share

All Articles