ExtJS集成Java API保存记录报错:Ext.form.Basic.updateRecord()需传入记录
Fixing "Ext.form.Basic.updateRecord(): A record is required" Error in ExtJS
Hey there! Let's break down why you're hitting that error and fix it step by step.
What's Causing the Error?
The updateRecord() method requires your form to be already linked to an Ext.data.Model (record) instance. In your current code:
- You're calling
dialog.updateRecord()before ensuring the form has a record associated with it - For new records (phantom records), you never create a new record instance and bind it to the form first, so there's nothing to update
Step-by-Step Fix
Here's the corrected code with clear explanations:
onSaveClick: function (button) { var dialog = button.up('window').down('form'); var record = dialog.getRecord(); var store = Ext.getStore('Routes'); // Handle new record scenario: create a new record if none exists if (!record) { record = Ext.create('YourModelName'); // Replace with your actual Model class name dialog.loadRecord(record); // Bind the new record to the form } // Now update the record with form data (no more missing record error!) dialog.updateRecord(record); // Check if we're saving the user record if (record === trac.app.getUser()) { record.save(); } else { if (record.phantom) { store.add(record); } store.sync({ failure: function (batch) { store.rejectChanges(); trac.app.showError(batch.exceptions[0].getError().response); } }); } button.up('window').close(); }
Key Changes Made:
- Check for existing record first: We grab the form's record immediately, and create a new one if it doesn't exist (this is critical for new entries)
- Bind new record to form: Use
loadRecord()to link the new record to the form before trying to update it - Explicitly pass record to updateRecord(): While ExtJS can infer the linked record, being explicit avoids ambiguity and ensures reliability
- Fixed code order: We now guarantee a record exists before attempting to update it
Additional Notes:
- Replace
YourModelNamewith the actual name of your ExtJS Model class (the one yourRoutesstore is configured to use) - If this form is used for editing existing records, make sure you call
loadRecord()on the form when opening the dialog (e.g., when the edit button is clicked) to bind the existing record upfront
内容的提问来源于stack exchange,提问作者Mayuri Gavali
相关产品推荐
相关产品推荐

