ExtJS 4 MVC Application: Form undefined error - forms

I am new to ExtJS 4 and am trying to implement a simple application in MVC style. The documentation is really good and this is one of the topics covered in the Form package guide, but it doesn't seem to work in my case.
The app basically can create, edit and delete articles.The creation and editing are in pop-up windows.
The pop-up window contains a form with a text field and html-editor.
Please click on the link below,this is the error in Google Chrome Console when I click on the submit button in the 'window'
http://www.freeimagehosting.net/mjig7
Here is the code which I've written
Model:
Ext.define('AM.model.Article', {
extend: 'Ext.data.Model',
fields: ['name', 'data'],
proxy: {
type: 'rest',
url: 'users/data',
reader: {
type: 'json',
root: 'myJaxBean',
successProperty: 'success'
},
writer: {
type: 'json'
}
}
});
View:
Ext.define('AM.view.New', {
extend: 'Ext.window.Window',
alias : 'widget.new',
title : 'New Article',
autoShow: true,
fieldDefaults: {
labelAlign: 'top',
msgTarget: 'side'
},
layout: 'fit',
bodyStyle:'padding:5px 5px 0',
width: '70%',
height: '40%',
initComponent: function() {
this.items = [
{
xtype: 'form',
anchor: '99%',
items: [
{
xtype: 'textfield',
name : 'name',
fieldLabel: 'Article Name',
anchor: '99%'
},
{
xtype: 'htmleditor',
name : 'data',
fieldLabel: 'Article',
anchor: '99% -25'
}
],
buttons: [
{
text: 'Submit',
handler: function() {
var form = this.up('form').getForm(), // get the basic form
record = form.getRecord(); // get the underlying model instance
if (form.isValid()) { // make sure the form contains valid data before submitting
form.updateRecord(record); // update the record with the form data
record.save({ // save the record to the server
success: function(user) {
Ext.Msg.alert('Success', 'User saved successfully.')
},
failure: function(user) {
Ext.Msg.alert('Failure', 'Failed to save user.')
}
});
} else { // display error alert if the data is invalid
Ext.Msg.alert('Invalid Data', 'Please correct form errors.')
}
}
},
{
text: 'Cancel',
scope: this,
handler: this.close
}
]
}
],
this.callParent(arguments);
}
});
and finally the code in my controller which makes the window visible
Controller:
.....
'viewport > panel > panel > tbar button[action=newarticle]' :{
click: this.newArticle
},
....
newArticle: function(button){
var view = Ext.widget('new');
},
Please point me in the right direction in case I am doing something wrong.
Thanks in advance.

Check the docs getRecord():
Returns the last Ext.data.Model instance that was loaded via
loadRecord
so it's clear that you haven't load any record, so you getRecord() returns undefined. And you are getting your error further.

Related

ExtJS 7.2.0 - Form submit not working in Chrome

I'm trying to upload a file using the code below:
window = Ext.create('Ext.window.Window', {
renderTo: Ext.getBody(),
bodyPadding: '20 10',
title: 'Upload file',
autoDestroy: true,
hidden: true,
modal: true,
layout: {
type: 'hbox',
align: 'middle',
pack: 'center'
},
items: [
uploadForm = new Ext.form.Panel({
items: [
file = new Ext.form.field.File({
xtype: 'filefield',
name: 'fileName',
fieldLabel: 'File',
allowBlank: false,
buttonText: 'Select file...',
})
]
})
],
buttons: [{
text: 'Cancel',
handler: function () {
upload.hide();
}
},
{
text: 'Upload',
handler: function () {
var form = uploadForm.getForm();
if (form.isValid()) {
form.submit({
url: 'upload',
waitMsg: 'Uploading your file...',
scope: this,
success: function (form, action) {
upload.close();
var json = JSON.parse(action.response.responseText);
if (json.success) {
Ext.Msg.alert('Success', json.message);
} else {
Ext.Msg.alert('Error', json.message);
}
},
failure: function (form, action) {
upload.close();
try {
var json = JSON.parse(action.response.responseText);
Ext.create('Ext.window.MessageBox').show({
title: 'Failure',
msg: json.message
});
} catch (err) {
Ext.create('Ext.window.MessageBox')
.alert('Failure', 'Failed to parse response');
}
}
});
}
}
}]
});
The code is working in firefox and opera and I get the response successfully, but in chrome on inspect network activity, the status is canceled and in the console I get the warning: Resource interpreted as Document but transferred with MIME type application/json. Therefore, the submit always returns failure, even though the file is uploaded. Can anyone please suggest how to fix this?

ExtJS - How to implement a container with its own isValid and hasInvalidField methods

For example, let's say we have three phone number fields: mobile, home, business, that we want to encapsulate as a contact item.
The phone number fields can be blank or, if not, have their own validity checks, the contact item has its own validity check in that at least one number must be provided.
I'm thinking about doing something like this:
Ext.define('My.widgets.contact', {
// To make use of Ext.form.Labelable mixin
extend: 'Ext.form.fieldcontainer',
xtype: 'contact',
alias: 'widget.contact',
requires: [
'My.widgets.phone'
],
mixins: [
// To link in isValid and other methods
'Ext.form.field.Field'
],
items: [
{
xtype: 'phone',
fieldLabel: 'mobile'
},
{
xtype: 'phone',
fieldLabel: 'home'
},
{
xtype: 'phone',
fieldLabel: 'business'
}
],
isValid: function() {
let valid = false;
if (this.callParent(arguments)) {
for (const item in this.getItems()) {
if (item.getValue() != '') {
valid = true;
break;
}
}
}
return valid;
}
});
Is this the right way to do this?
The idea is to have the minimum extra fluff. For example, form panels have dockable, button, header and footer elements which aren't needed. Also, I've read about problems in ExtJS having multiple form items on a page, so want to avoid sequential or nested forms on a single page.
Another approach might be to extend Ext.container.Container with the Ext.form.Labelable and Ext.form.field.Field mixins. Would that work?
In ExtJs fieldcontainer have method query so you use this to check your Validation.
Query retrieves all descendant components which match the passed selector. Executes an Ext.ComponentQuery.query using this container as its root.
As you have used this.getItems() it will return all the items of fieldcontainer as whatever it contain. In this case you need to maintain condition to check getValue() only for fields.
Query will return only that component as you want to check.
I have created an Sencha Fiddle demo hope this will be help you to achieve your solution.
//Custom xtype for phone
Ext.define('Ext.form.field.Phone', {
extend: 'Ext.form.field.Text',
alias: 'widget.phone',
xtype: 'phone',
maskRe: /[\d\.]/,
regex: /^\d+(\.\d{1,2})?$/,
maxLength: 11,
enforceMaxLength: true
});
//Contact details
Ext.define('ContactForm', {
extend: 'Ext.form.FieldContainer',
alias: 'widget.contact',
xtype: 'contact',
flex: 1,
layout: 'vbox',
defaults: {
xtype: 'phone'
},
items: [{
fieldLabel: 'mobile'
}, {
fieldLabel: 'home'
}, {
fieldLabel: 'business'
}],
isValid: function () {
return this.query('phone[value=""]').length > 0;//you can also use like this { this.query('phone[value!=""]') };
}
});
//Panel this will contain contact
var panel = Ext.create('Ext.panel.Panel', {
itemId: 'myPanel',
bodyPadding: 5,
width: 300,
renderTo: Ext.getBody(),
title: 'Contact Details',
items: [{
xtype: 'contact'
}],
buttons: [{
text: 'Submit Details',
handler: function () {
var contactFrm = this.up('#myPanel').down('contact'), //you also use like this this.up('panel').down('contact')
title = 'Success',
msg = 'Good you have entered the contact details..!';
if (!contactFrm.isValid()) {
title = 'Error';
msg = 'Plese enter at least one contact detail..!'
}
Ext.Msg.alert(title, msg);
}
}]
});

ExtJS: How to add new object to Store and send POST request to RESTful server

I am creating a Todo application with ExtJS 6. My application should fetch data, here are Todos list from a Server using RESTful web service, and add new Todo task and send this new task to store in the Server. For the fetching part, it was successfully to show the data from the server, but I still facing a problem with the adding data and send back to the server.
Here are components that I have:
A View called ToDoList which contains 1 textfield and 1 button to add new Todo task:
Ext.define('ToDo.view.toDoList.ToDoList',{
extend: 'Ext.panel.Panel',
requires: [
'ToDo.view.toDoList.ToDoListController',
'ToDo.view.toDoList.ToDoListModel'
],
xtype: 'app-todoList',
controller: 'todoList',
viewModel: {
type: 'todoList'
},
items: [{
xtype: 'container',
items: [{
xtype: 'container',
layout: 'hbox',
cls: 'task-entry-panel',
defaults: {
flex: 1
},
items: [{
reference: 'newToDo',
xtype: 'textfield',
emptyText: 'Enter a new todo here'
},
{
xtype: 'button',
name: 'addNewToDo',
cls: 'btn-orange',
text: 'Add',
maxWidth: 50,
handler: 'onAddToDo'
}]
}]
}]
});
My ViewModel called ToDoListModel which contains store object named todos, which including 3 parameters: id, desc and done.
Ext.define('ToDo.view.toDoList.ToDoListModel', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.todoList',
stores:{
todos: {
fields: [{
name: 'id',
type: 'string'
},
{
name: 'desc',
type: 'string'
},
{
name: 'done',
type: 'boolean'
}],
autoLoad: true,
sorters: [{
property: 'done',
direction: 'ASC'
}],
proxy: {
type: 'rest',
url: 'http://localhost:8082/RegistrationForm/tasks/',
reader: {
type: 'json'
},
writer: {
type: 'json'
}
}
}
}
});
And My ViewController called: ToDoListController looks like:
Ext.define('ToDo.view.toDoList.ToDoListController', {
extend: 'Ext.app.ViewController',
alias: 'controller.todoList',
views: ['ToDo.view.toDoList.ToDoList'],
init: function(){
var me = this;
this.getViewModel().data.todos.load(function(records){
Ext.each(records, function(record){
console.log(record);
me.addToDoToView(record);
});
});
Ext.getBody().on('click', function(event, target){
me.onDelete(event, target);
}, null, {
delegate: '.fa-times'
});
},
onAddToDo: function () {
var store = this.getViewModel().data.todos;
var desc = this.lookupReference('newToDo').value.trim();
**HOW CAN I ADD NEW OBJECT TO STORE AND PERFORM POST REQUEST HERE TO THE SERVER**
},
addToDoToView: function(record){
this.view.add([{
xtype: 'container',
layout: 'hbox',
cls: 'row',
items: [{
xtype: 'checkbox',
boxLabel: record.get('desc'),
checked: record.get('done'),
flex: 1
},
{
html: '<a class="hidden" href="#"><i taskId="'+ record.get('id') + '" class="fa fa-times"></i></a>'
}]
}]);
},
onDelete: function(event, target){
var store = this.getViewModel.data.todos;
var targetCmp = Ext.get(target);
var id = targetCmp.getAttribute('taskId');
store.remove(store.getById('id'));
store.sync({
success: function(){
this.view.remove(targetCmp.up('.row').id)
},
scope: this
});
}
});
And when the user click to the Add Button, the function onAddToDo() in the Controller will be call, here it will get the new task from the textfield (here desc variable), create a new Todo object and then send this object to the server using POST request. My service in the server will look like:
//ADD new Todo
#RequestMapping(value = "/tasks/", method = RequestMethod.POST)
public ResponseEntity<Void> createUser(#RequestBody Todo todo, UriComponentsBuilder ucBuilder) {
//add this new Todo object to the database
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
What I would like to do is after user entering new Task, I will pack it as new object, structure look like: {'id'=1, 'desc'='what user entered','done'=false} and send it as the RequestBody of the POST method to the server. Can anyone guide me how can I do that? Thank you
You can add the record to the store by using add() function.
store.add({id:1, desc:'what user entered',done:false});
Then you can call sync() function which will call the create api of the store and send the inserted data to server.
store.sync();
sync() synchronizes the store with server in 3 cases that are new record creation, record updation and record deletion. So here when we add a new record and call sync(), it will send only the newly added record to the server as it is not present at server to the URL specified in store

ExtJS 3.4 - Select row after load store of my gridpanel

My grid panel:
new Ext.grid.GridPanel({
title: "Utilisateurs",
layout: 'fit',
style: marginElement,
columns: mesColonnesUtil,
id: 'gridPanelUtil',
width: '70%',
colspan: 2,
collapsible: false,
layout: 'fit',
autoWidth: true,
monitorResize: true,
height: 200,
store: storeUtil,
stripeRows: true,
selModel: new Ext.grid.RowSelectionModel({
singleSelect: true
}),
listeners: {
click: function () {
this.selModel.getSelected();
}
}
});
My store:
var storeUtil = new Ext.data.JsonStore({
proxy: proxyGrUtil,
baseParams: {
method: 'storeUtil',
gr: ''
},
autoLoad: true,
fields: ["Nom", "Prenom", "LDAPUser"],
root: "rows",
totalProperty: "total",
successProperty: "success"
});
My combobox with select event, I load my grid panel with params:
{
xtype: 'combo',
store: storeGrUtil,
id: 'comboGrUtil_GrUtil',
width: 300,
valueField: "id",
displayField: "lib",
triggerAction: 'all',
mode: 'local',
listeners: {
select: function () {
Ext.getCmp('gridPanelUtil').store.load({
params: {
gr: Ext.getCmp('comboGrUtil_GrUtil').getValue() // this the value of items selected combobox
}
})
}
}
}
After this event, I can't select a row in my grid panel, why ?
I don't understand.
The problem is the call of the load event of the store. You have to execute the row selection in this event and not after the call. If I remember well, the grid is completely loaded in this event. Take a look of the code tag above.
If it not working, I suggest to take a look at other event. Maybe with rowinserted of the gridView.
var storeUtil = new Ext.data.JsonStore({ proxy: proxyGrUtil,
baseParams: { method: 'storeUtil', gr: '' },
autoLoad: true,
fields: ["Nom", "Prenom", "LDAPUser"],
root: "rows",
totalProperty: "total",
successProperty: "success",
listeners:
load: function(e, records, options){
Ext.getCmp("gridPanelUtil").getSelectionModel().selectRow(maLigneASelectionner);
}
}
});
you need to use the selectionModel of the grid, maybe you can pass a callback when calling load to the store
store.load({
callback: function(records, operation, success) {
//operation object contains all of the details of the load operation
//records contains all the records loaded
console.log(records);
}
});
the you can call
grid.getSelectionModel( ).select(object/index);
//you need to pass record instance or index

panel overlapping while loading them dynamically sencha

I have several tabs on toolbar each of them having seperate handler , i have called seperate function of each handler . The code is as below
Ext.define('myco.view.User', {
extend: 'Ext.Container',
config: {
scrollable: true,
items: [{
xtype: 'panel',
id: 'User'
},
{
docked: 'top',
xtype: 'toolbar',
items: [{
text: 'My Profile',
handler: function() {
var panel = Ext.getCmp('User'),
tpl = new Ext.XTemplate([
'<div class="demo-weather">',
'<tpl for=".">',
'<div class="day">',
'<div class="date">{username}</div>',
'<tpl for="weatherIconUrl">',
'<img src="{value}">',
'</tpl>',
'<span class="temp">{tempMaxF}°<span class="temp_low">{tempMinF}°</span></span>',
'</div>',
'</tpl>',
'</div>'
]);
panel.getParent().setMasked({
xtype: 'loadmask',
message: 'Loading...'
});
Ext.Ajax.request({
url: 'http://localhost:8000/api/myapp/user/',
method:'GET',
callbackKey: 'callback',
defaultHeaders : 'application/json',
params: {
//key: '23f6a0ab24185952101705',
//q: '94301', // Palo Alto
format: 'json',
//num_of_days: 5
},
success : function(response, opt) {
dataarray = Ext.decode(response.responseText);
weather=dataarray.objects;
panel.updateHtml(tpl.applyTemplate(weather))
panel.getParent().unmask();
},
failure : function(response, opt) {
Ext.Msg.alert('Failed', response.responseText);
panel.getParent().unmask();
}
});
}
},
{
text:'login',
handler: function() {
var main = new Ext.Panel({
title: 'My first panel', //panel's title
fullscreen: true,
//the element where the panel is going to be inserted
//html: 'Nothing important just dummy text' //panel's content
items:[
{
xtype: 'fieldset',
title: 'Login',
items: [
{
xtype: 'textfield',
label: 'Username',
name: 'username',
id : 'username'
},
{
xtype: 'passwordfield',
label: 'Password',
name: 'password',
id : 'password'
}
]
},
{
xtype: 'button',
text: 'Send',
ui: 'confirm',
handler: function() {}
}
]
});
panel.add(main);
alert('test');
}
}
]
}]
}
});
Now when i click on the tab , previous panel will not be cleared and data will get overlapped there ..... like when i click My profile profile is listed there , after that when i click login profile+ login both will be loaded one overlapping another ...
You should use Ext.tab.Panel for this, define your profile and login panels as items of a parent TabPanel, the switching will be handled by ExtJS. You can use the activate event of a Panel to add custom logic to execute when your tabs get activated.