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();

Saturday, 13 September 2014

Spring MVC Test cases with Annotated Beans Using Mockito

Annotations make java developer's life easy and also it feels like annotation based classes are complex for unit testing. But in reality it is quite simple. There are couple of ways through which you can test autowired/spring based classes.

Method 1:
You can use AnnotationConfigApplicationContext to load application context. Instantiate application context, register annotated classes and test your methods. Here is the sample code

Method 2:
Even though preceding method is an easiest way to test annotation based spring classes, if your test case becomes complex and if you want to mock actual request etc, you better move to mocking framework instead of basic one. I have used mockito with spring-test to mock actual requests with spring beans. Here is the sample code

In the above sample code we have used spy to mock real objects, if you just want to mock it you can use mock annotation instead of spy.

For this you need mockito and spring-test jars. Following are the minimal poms for the both respectively.
     <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
  </dependency>
            <dependency>
           <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.0.0.RELEASE</version>
          <scope>test</scope>
  </dependency>

Here is the complete code with both the testing methodologies.
spring-test-template using mockito

Wednesday, 10 September 2014

Extjs 4 : Delegating Events

Though ExtJS supports excellent event handling mechanism, sometimes we may want to handle custom events on some HTML components. For example if you want to handle hyperlink click whereas hyperlink is embedded in html code(using html tag or xtemplate), then you may need to delegate events to the component that you have created using 'delegate'. You can use simple hack to handle this.

  • Create HTML text using html tag or xtemplate. 
  • Assign classname/id to the component to which you want to attach an event. 
  • Attache event to the Ext body and delegate to the specific class/id show in below.
  • Make sure that class name or id is unique. 

Here is the fiddle to fiddle around with it.
Sample snippet

Friday, 29 August 2014

ExtJS4 Dynamic Grid

In the MV* patterns, model refers to the fixed state of content. If state changes then the benefit that comes out of this model is trivial. Some times it could be a case that model is dynamic, in such a case ExtJS grid should be constructed dynamically. Here is how the sample code goes...

ExtJS4 grid has a method called reconfigure which takes store and columns as input arguments and restructures grid. These columns and store depends on the field data which a part of model. Since model is dynamic lets create field data dynamically along with the actual data and send it to the server.



Here is the structure from server

Thursday, 21 August 2014

ExtJS4: Missing HasOneInstance While Decoding JSON

Sometime ExtJS plays dumb role in throwing exceptions. It ignores exceptions instead of handling them, especially while dealing with associations. 

Whenever ExtJs loads data throw proxy into model, it decodes json data into respective models including associations. In case of hasMany association it creates <association-name>Store and in case of hasOne association, it creates <association-name>HasOneInstance store and decodes data into corresponding stores. 

But here are some cases where ExtJS may miss these conversions without throwing any error. 
  • If your association name is in camel case and if you have defined its association with its basic properties like type and model, then ExtJs generates rest of the properties by default. While generating default properties it converts camel case text into lower case and associate it to the association. 
   For example if you have defined a hasOne association as follows, 
       associations: [{ type: 'hasOne', model: 'homeAddress' }]
  It generates association_key as homeaddress (converts camel case into lower case) and maps this key      with the json object which will be in camel case. Because of mismatch in name, it doesn't create homeAddressHasOneInstance object in the parent object. 
  • If you don't use association's model explicitly and miss its inclusion in controller then ExtJS doesn't complain about the model and store conversion will not happen for that association. 

Saturday, 16 August 2014

Logging in Eclipse Birt

Birt is one of the best open source projects that provides data visualization and reports for rich internet applications. But logging is one of the common problems while developing reports in BIRT report engine. Here is the small hack that I have used in birt global script to log my data.
  • Select initialize Script in birt(Select report, open report in Birt Report Design perspective select report and go to script tag, on the top of the screen, you can select initialize script). 
  • Since Birt uses java logging, you can use following script in initialize script to log the message
  • 
    
    
    
    importPackage(Packages.java.lang); function log(msg){ System.out.println("Info : " + msg); } reportContext.setGlobalVariable("log", log)
    
    
  • Use log function in other scripts to log message. 
Note : If you cant find the console in eclipse, open eclipse in console mode. (To open eclipse in console mode, open eclipsec.exe instead of eclipse.exe). 

You can also use Log4j properties in birt reporting. Here is the detailed explanation.   

Wednesday, 13 August 2014

Eclipse : Server Error - Resource **** Does Exist

Today I was trying to configure my development environment in newly released Eclipse version: LUNA. Suddenly I faced a  problem in configuring server. I was just using simple tomcat which I had used it like hundred times. Only change I did was to modify small entry in some configuration file and then eclipse started crying. I tried many combinations, changed different tomcat servers and to test the tomcat server I moved into older eclipse. Finally I found that its a bug in eclipse. Its not with the new eclipse version but there with every eclipse version.

Here is the error...
Reason:
Could not save because file has been modified since the start of editing eclipse



Here is the solution to my problem. Its the name that has caused the problem. While naming the server I have used server location as the name of the server.

I used server name as 'C:\NGPS\apache-tomcat-7.0.27'  and because of the back slashes in the name, it was failing to add new server. Moreover message that was throwing matched in my case and I ended up in doing permutations and combinations. Simple workaround for this problem is to change tomcat name to a simple string with alpha-numerics. Once you change the name, close 'Add new server window' and try to add it agian.

Wednesday, 2 July 2014

ExtJS4 : Copying data from one grid to another.

Copying content into clipboard is a security hole. As a result no simple javascript method is available to write content into clipboard. Except IE, no other browser supports this without any plugins/flash. But if you just want to copy one row of grid and paste it into another grid you can use following hack.(Two grid stores should use same model). 

  • Select a row and provice ExtJS context menu, in action get selected record. It will be in the form of model. Convert this model object into JSON object using getData(true) method on the model. It converts model object into json including association data. 
  • Now your task is to persist this data in user's environment. For this you can use HTML5 web storage. It is simple, faster and secure. But it doesn't work on IE7 and earlier versions.
                   localStorage.setItem("json-object", model.getData(true));
  • Above code stores model object into client's browser in the form of string. 
  • When you wan to handle paste, fetch the stored data string and decode into JSON object. 
                  var jsonObject =  Ext.Deode(localStorage.getItem("json-object"));
  • After retrieving jsonObject convert this back in to model and push it into grid's store. 
            Check this to convert jsonObject into model.


Extjs4 : Converting JSON Object into model

ExtJS provides getData(true) method on model to convert model object into JSON Object including associations. But other way is not a straight forward thing in ExtJS4. If you have JSON and want to convert into model, one dirty way could be create a store with memory proxy, input JSON object as data object and access first record in the store. But this method is not always fruitful. If your JSON has associated data, this method will not work properly. Especially in case of hasOne association case, it never creates *HasOneInstance objects using this method.

I found similar API in Sencha touch 2.2.1 and modified a bit to make it work in ExtJS 4.2.1