Sunday 25 January 2015

ExtJS5 - Codemirror Integration

Being one of the leading frameworks for RIA, I was expecting ExtJS 5 with an inbuilt code editor. There are some add-ons available for ExtJS3 and ExtJS4 by extending form field base so that they can make it in par with form. But it is more restricted using in that way. We need to compromise with the layout if you are not using it on any other component. Instead we can use a simple hack to make it work seamlessly on any ExtJS components.

Since code mirror requires div or textarea, we can make use of ExtJS' 'box' to get handle of div. Next level complexity comes with form support and layouting. Here is the idea

  • Create a custom component by extending container of panel(As per requirement)
  • Use codemirror's configs as configs of custom component
  • Add following components as items
    • box : Since it provides a div, we can use it to render code mirror component
    • field: Add this item to your custom component and make it hidden. Replicate all the changes/actions on this field component whenever there are changes/actions on the codemirror. 
  • Initialize codemiror field 'afterrender' of custom component with the configs that are defined on it. 
Form Support
ExtJS form comes with the additional properties like dirty change, load/get/update Record features  so that we can load the model into the form and work with it. Whenever we add our custom component to form, form will be able to identify the field item which is the part of our custom component. Since ExtJS form fires every action on our field, we can get the handle form actions. 

For example if there is a change event on code mirror, we will be able to find out whether its a dirty or not but how can we tell form that it is dirty? Thats where we bring our field into action. Call setDirty function of field and fire dirty change event from that. Rest will be taken care by form. Sam e applies with the other actions. 

Layout Support
Since it is our custom component, we don't need to worry much. 

Controlling Size
Code mirror has a pre-configured height(300) in its css and there is a method to set custom height and width. We can pass these values as configs to our custom component and call setSize method of codemirror. 


I have tried a custom component with remote validation features. You can find source code at the following location. This is just a prototype code to show how we can make codemirror work with ExtJS without affecting much from its version changes. You can take the idea and customize it as per your needs. 



Tuesday 20 January 2015

Selection of Div Content on ExtJS(Select ALL)

Here is a simple code snippet to show data on view with select all enabled container.



Sunday 11 January 2015

Workaround : Ext JS 5.1.0 checkboxmodel 'deselect' defect

ExtJS 5.1.0 is having a bug in checkboxmodel. While using checkbox model, it is not firing deselect/selection change when the model is 'MULTI' which is a default value.

Use following snippet to make it work.

 selModel: {
          selType: 'checkboxmodel' 
          model : 'SIMPLE'

}

Thursday 1 January 2015

model Deletion in ExtJS 5

While I was working with ExtJS 5, I faced a problem in deleting a record. 'erase' is a method in ExtJS 5 to delete a model, which is a counter part of 'destroy' method in ExtJS 4. I tried 'erase' method on model to delete it using rest and it was not sending any DELETE request to the server. Here is the sample code snippet
var user = Ext.create('User', {
   extend : 'Ext.data.Model',
   fields : ['id', 'name', 'email'],

   proxy : {
    type : 'rest',
    url : '/users'
   }
  });

user.set('id', '12345');
user.erase();

In the preceding function call, will not be any XHR request with 'DELETE' method as we expect, if you are using ExtJS-5.1.0. In ExtJS 4, if there is any change in the ID property, it calls a function(changeId) that does house keeping of a model hence, model's phantom becomes false whereas in ExtJS5, user.set('id','12345') will not change user's phantom to false(When we create model using Ext.create, default phantom value will be true). Since it is a phantom record(new record), it will not call any 'DELETE' request.

Here is a simple hack to handle this problem. We hope there will be a fix in their future releases.

user.set('id','12345');
user.commit(); // user.phantom becomes false
user.erase();
or
user.set('id','12345');
user.phantom = false; //Make phantom as false manually
user.erase();