PDA

View Full Version : updateRecord, beginEdit, and endEdit



bmacdon1
9 Feb 2012, 8:47 AM
Version: 4.0.7 and 4.1

In the following code from Extjs, there is a call to beginEdit() and endEdit() and a call to record.set(obj).


Ext.define('Ext.form.Basic', {
...
updateRecord: function(record) {
var fields = record.fields,
values = this.getFieldValues(),
name,
obj = {};

fields.each(function(f) {
name = f.name;
if (name in values) {
obj[name] = values[name];
}
});

record.beginEdit();
record.set(obj);
record.endEdit();

return this;
},
...
});


Isn't this redundant? As I understand it when you pass an object to set() function in Ext.define('Ext.data.Model', {}); the code will do the beginEdit and endEdit. Is it possible there could be a potential problem?



Ext.define('Ext.data.Model', {
.....
set: function(fieldName, value) {
var me = this,
fields = me.fields,
modified = me.modified,
modifiedFieldNames = [],
field, key, i, currentValue, notEditing, count, length;

/*
* If we're passed an object, iterate over that object.
*/
if (arguments.length == 1 && Ext.isObject(fieldName)) {
notEditing = !me.editing;
count = 0;
fields = me.fields.items;
length = fields.length;
for (i = 0; i < length; i++) {
field = fields[i].name;
if (fieldName.hasOwnProperty(field)) {
if (!count && notEditing) {
me.beginEdit();
}
++count;
me.set(field, fieldName[field]);
}
}
if (notEditing && count) {
me.endEdit(false, modifiedFieldNames);
}
} else {
fields = me.fields;
if (fields) {
field = fields.get(fieldName);

if (field && field.convert) {
value = field.convert(value, me);
}
}
currentValue = me.get(fieldName);
me[me.persistenceProperty][fieldName] = value;

if (field && field.persist && !me.isEqual(currentValue, value)) {
if (me.isModified(fieldName)) {
if (me.isEqual(modified[fieldName], value)) {
// the original value in me.modified equals the new value, so the
// field is no longer modified
delete modified[fieldName];
// we might have removed the last modified field, so check to see if
// there are any modified fields remaining and correct me.dirty:
me.dirty = false;
for (key in modified) {
if (modified.hasOwnProperty(key)){
me.dirty = true;
break;
}
}
}
} else {
me.dirty = true;
modified[fieldName] = currentValue;
}
}

if(fieldName === me.idProperty && currentValue !== value) {
me.fireEvent('idchanged', me, currentValue, value);
}

if (!me.editing) {
me.afterEdit([fieldName]);
}
}
},
.....
});


Bryan

mitchellsimoens
9 Feb 2012, 9:36 AM
This is best used when you need to do multiple record.set calls

bmacdon1
9 Feb 2012, 12:57 PM
It's redundant code. In this situation there are two calls to beginEdit and two calls to endEdit.