Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

I try to add an Export button in ExtJS, it should do URL encoding for the search parameters and pass the generated query string to the export URL.

When testing above code in Chrome, following error happened:

Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode


The reason is let keyword is not allowed in non-strict mode for this version of Chrome.

One solution is to change let to var. (But it isn't good)
The better solution is to add "use strict" statement.

So the final code should be

Ext JS Image Reload

In Ext JS, to reload or refresh an image, we can call updateSrc() method, and also setSrc() method is working as well.

 

And if the requested image is cached, we need use cache buster to avoid loading cached image.

 

Note that I found another post saying to use doComponentLayout method to refresh an image, but this method is deprecated since version 4.1.

Ext JS 6 Modern Set Field Renderer in itemTpl

In Ext Js 6 Modern (or Sencha Touch), if Ext.grid.plugin.RowExpander plugin is enabled for Ext.grid.Grid, there will be a expander icon (plus icon) at left side of each row. Clicking the expander icon will expand the selected row, and more detail will be displayed. This detail page can be set by itemConfig.body.tpl option.

 

Like following code

 

And the tpl option is actually XTemplate, so the above code can be written as following form as well:

 

Note that here XTemplate cannot be replaced by Template class (using Template will display raw literal value as {...})

 

 

To use a renderer function for field in the template string, we need add a member function to the template

Then the template can be changed like this

 

 

Ext JS 6 Modern Set TabBar Scrollable

By default in Ext JS 6 Modern version (or Sencha Touch), the TabPanel's TabBar is not scrollable. That means if opened tabs is too many that the TabBar will push the earlier tabs to left side, then they will become invisible anymore (because it's not scrollable)

To make it scrollable, we need set tabBar.scrollable config to true when creating TabPanel

Then we can drag the TabBar to scroll it.

 

By default the scrollable is 'horizontal' , and of course you can set it to 'vertical' .

 

 

 

Ext JS 6 Duplicate Entity Name Error

When I reopened Account Profile tab after closing it, I got following error (shown in browser's console)

Duplicate entity name "AccountProfileModelList": AccountProfileModelList and AccountProfileModelList(…)

 

In this page, I defined a Model named AccountProfileModelList

 

When opening the Account Profile tab first time, the AccountProfileModelList will be defined. And when opening the tab second time, the AccountProfileModelList will be defined again. This is why the error appeared.

What's interesting is that In Ext 5 above scenario won't cause any error, but in Ext 6, a model cannot be defined twice anymore.

 

To fix this issue, we need use typeof(AccountProfileModelList) == 'undefined'  to check whether the model is already defined, if it's defined the model creation code won't be executed.

So the above code should be changed to

 

A more Ext way to detect whether a Ext class is already defined is using Ext.ClassManager.isCreated(className)

Above code can also be changed to

 

Ext JS 6 Invalid Component Id

In Ext 6, I got following error

ExtJs invalid component id "profile.ModifyUserBasicInfo"

 

Following is code producing the error

 

This error doesn't appear in Ext 5, after upgrading to Ext 6, it appears now.

 

It is caused by the id property cannot contain "." symbol, but the id property we used here is profile.ModifyUserBasicInfo

To fix this issue, we can replace all "." to "-".

 

Following link from describes the id property naming rule

http://docs.sencha.com/extjs/6.2.1/classic/Ext.window.Window.html#cfg-id

The original quote

Note: Valid identifiers start with a letter or underscore and are followed by (optional) additional letters, underscores, digits or hyphens.

 

 

 

Ext JS form load custom object

I have a custom object currentUser, it contains bank account name, mobile number,  and I want fill the form with currentUser's properties.

 

To load a custom object,  first define a Model, then instantiate the Model with custom object, next call loadRecord with the model object. Note that I tried Ext.form.Panel loadRecord(), it's not working for me, Ext.form.Basic should be used here. So we need call getForm() to get Ext.form.Basic

 

Another example

http://jsfiddle.net/el_chief/HBah5/4/

 

 

 

Fabric.js set text color

Fabric.js is a powerful javascript canvas library which wraps html5 canvas operation, and introduces objects (text, shape, path, etc.) to developer.

There is a common method set() for object, it can be used to set property of an object. But if you want to change text color, text.set('color', '#0f0') will not work.

To set color of text, we need modify the fill attribute or use setColor() method (only available for text object)

Or

 

 

Date format in javascript

Formatting date is a common task, like converting timestamp to "yyyy-MM-dd hh:mm:ss" format (e.g. 2013-03-03 12:00:00)

Date.format function can do this job

But it only works in Chrome, but not work in IE and Firefox.

Here we can use Moment.js library to format the date (Moment.js is a cross-browser date library)