Adding store events to your controller in Extjs 4.1

Ok so you set out to code your new extjs app using MVC and now you want to do something like add a ‘datachanged’ or ‘load’ event to your store. This is common stuff right, you know exception handlers, so you open up Google or stack overflow and start searching, but what you get is a whole lot of differing opinion on how to accomplish this trivial task. There are a few things that you already know; they are your ground rules so to say. Come hell or high water you will not add listeners to your store, you want all of the methods (or actions) to go in the controller.  So what are you to do?

First let’s look at our sample store:

Ext.define('sample.store.MainStore', {
    extend      : 'Ext.data.Store',
    model       : 'sample.model.MainStore',
    pageSize    : 100,
    remoteSort  : true,
    proxy: {
        type: 'ajax',
        extraParams: {
            start        : 0,
            limit        : 100
        },
        url: 'someurl.php',
        method: 'POST',
        reader: {
            type: 'json',
            root: 'data',
            totalProperty:'results'
        }
    },
    autoLoad: true
});

… and every store has a model, right …

Ext.define('sample.model.MainStore', {
    extend: 'Ext.data.Model',
    fields: [
        'some_primary_key',
        'some_data_element',
    ]
});

Now for the Pièce de résistance – our controller:

Ext.define('sample.controller.Main', {
    extend     :  'Ext.app.Controller',
    models    :  ['MainStore'],
    stores      :  [' MainStore '],
    views       :  ['Main'],
    requires  :  ['Ext.MessageBox'],
    init: function() {

        Ext.getStore('MainStore').addListener('load',
this.onMainStoreLoad,
 this);
        Ext.getStore('MainStore').addListener('datachanged',
this. onMainStoreDataChange,
 this);

        this.control({
               //  A sample combo box select function
            'combo[action=sampleSelect]':{
                select:this.onComboSelect
            },
	//  A sample button click function
            'button[action=sampleAction]':{
                click:this.onButtonClick
            }
        });
    },

// This is where all the functions go

    onComboSelect: function(combo) {
        	console.log('ComboBox Selected, the value is:' + combo.getValue()) ;
    },

    onButtonClick: function(me){
console.log('Button Clicked');
    },

    onMainStoreLoad: function(me,records,success){
if(!success){
     Ext.MessageBox.show({
        title   : 'Data Load Error',
        msg   : 'The data encountered a load error, please try again in a few minutes.'
                });
            }
    },

    onMainStoreDataChange: function(me){
console.log('Hey the store data just changed!');
    }

});

In closing there are a few things that I learned in this exercise that I would like to point out so you don’t repeat my mistakes.

  1. When you look at the API for the store, and you take one of the events (load for example is here http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-event-load ) you can pass the parameters in the function in the controller.

Lies, damn lies, and JavaScript statistics!

Ok I have been doing some statistical analysis of values in arrays lately (because it’s fun!) and wanted to pass on some of the more popular equations, along with what they mean, or what they mean to tell you.

Mean – Many of you no doubt know the mean as the average of a set of numbers, you calculate this by summing the values in the data set and dividing the sum by the number of values. This seems straight forward, and the concepts below rely on the mean so it is crucial that this be calculated. Ext.Array (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.Array-method-mean) has a handy way of calculating the mean value of a dataset stored in what else – an array.

In the world of probability the mean is also known as the expected value, and provides a good idea of where the next value will fall.

Variance – Now that you have the mean value of the array, you can start to look for the variance in your dataset. The variance is the measure of how far the numbers in your dataset are spread out. The baseline for this is the mean, which is also known as the expected value! Variance is calculated by taking the mean and subtracting that number from the value, then squaring the difference (between the mean and the value) for each of the numbers in your dataset. Wait! You are only half way there! Once you complete that task you should have a new dataset comprise of squared differences, now to get the variance just calculate the mean of the dataset of squared differences.

Standard Deviation – OK, now that we have worked out the mean, and the variance, what is standard deviation, and what do we plan to do with it. Before I explain, let’s take a look at the chart below:

In this chart you see the greek symbols for mean (μ)  and standard deviation (σ) . The type of curve represented by the chart above is named the bell curve. So you may be asking yourself what standard deviation is right — well, it is the square root of the variance and shows how spread out a set of numbers is. In the bell curve you can see that 34.1% of the dataset falls within -1 standard deviation and 34.1% falls within +1 standard deviation of the mean. This dataset can be categorized as normalized.

Frequency – Now let’s talk about frequency, which is commonly expressed as a histogram, or a chart that contains ‘bins’ and the number of items per ‘bin’.

Now, how do you go about calculating the values above given a set of values in a JavaScript array?

Luckily, I stumbled across some handy JavaScript snippets from Larry Battle (http://bateru.com/news/2011/03/javascript-standard-deviation-variance-average-functions/ ), and of course when you couple that with some extjs Array singleton functions you get an easy to use list of stats functions.

Average (mean)

var avg = Ext.mean(yourArray);

Variance

// Function returns variance for an array of numbers
function  variance(arr){
	// Make sure that your input is of type array
if (!Ext.isArray(arr)) {
return false;
}
	var avg = Ext.mean(arr);
	i = arr.length,
	v = 0;

	while (i--){
		v+= Math.pow((arr[i] – avg), 2 );
	}
	V /= arr.length;
	return (v);
}

Standard Deviation

function stdev(arr){
	// Make sure that your input is of type array
if (!Ext.isArray(arr)) {
return false;
}
	var sd = Math.sqrt(variance(arr));
return sd;

}

Frequency

function histogram(arrayOfNumbers){
var bins = 10,
histogramDataValues = arrayOfNumbers, // Source Values
i = 0,
over = 0,
under = 0,
binContent = [],       // Becomes the data array
delta;

var lowestNumber = Ext.Array.min(histogramDataValues),
highestNumber = Ext.Array.max(histogramDataValues),
delta = Math.abs((lowestNumber - highestNumber) / bins);

// Set the initial value of all bins to zero
// and populate the categories data based on the deltas

for (; i <= bins; i++) {
binContent[i] = 0;
histogramDataValues[i] = lowestNumber + Math.round(100 * (i - 1) * delta) / 100;
}

// Populate the bins with the
for (i = 0; i < histogramDataValues.length; i++) {

if (histogramDataValues[i] < lowestNumber) {
under++;
}
else if (histogramDataValues[i] >= highestNumber) {
over++;
}
else {
var ndata = histogramDataValues[i] - lowestNumber,
thisBin = Math.floor(ndata / delta);

thisBin = Math.abs(thisBin);
binContent[thisBin]++;
}
}
return binContent;
}

Random thoughts on code errors after teaching two different languages in one semester

When I was a bit younger we used to play a game called Pitfall where you would guide your player across various obstacles, including chomping alligators, deadly quicksand, and rushing rivers. In programming there are also pitfalls, and they may not be as bad as an alligator pit, but they will consume your time and leave you drained at the end of the day.

Software errors will always happen, it comes down to how quickly you can identify them and correct them that make all the difference. Those that argue that code should be written error free the first time have obviously not written much code. We can all agree that production code should be error free, but to achieve error free code means you will have to identify and eliminate errors.

I wanted to share a few thoughts on how to cut down on errors in your code.

Use proper indentation, and formatting. This is almost never taught in class, and most of the books that I have seen even contain code that runs and is compact but is not really easy to read.  It’s important to write all your code as if you are going to spend the rest of your career maintaining it.

This sounds great but how can I implement it? Let’s take a look at some code.

I like to use a tab for each block of code. Look at the difference between block a and block b.

A

Ext.override(Ext.form.Radio, {
        setValue : function(v) {
            if (typeof v == 'boolean') {
                Ext.form.Radio.superclass.setValue.call(this, v);
            } else if (this.rendered) {
                var els = this.getCheckEl().select('input[name=' + this.el.dom.name + ']');
                els.each(function(el){
                    if (el.dom.value == v) {
                        Ext.getCmp(el.dom.id).setValue(true);
                    } else {
                        Ext.getCmp(el.dom.id).setValue(false);
                    }
                }, this);
            }
            return this;
        }
    });

B

Ext.override(Ext.form.Radio, { setValue : function(v) {
if (typeof v == 'boolean') {
Ext.form.Radio.superclass.setValue.call(this, v);
} else if (this.rendered) {
var els = this.getCheckEl().select('input[name=' + this.el.dom.name + ']');
els.each(function(el){
if (el.dom.value == v) {Ext.getCmp(el.dom.id).setValue(true);
 } else {Ext.getCmp(el.dom.id).setValue(false);
}}, this);}
return this;
} });

Hard to believe that’s the same code right? Now imagine this throughout a file containing a thousand, ten thousand, hundreds of thousands of lines of code.

Use curly braces even when you don’t have to. Wait won’t that bloat my code? I’ve been taught that I should keep things as compact as possible what’s up with this. Let me explain in code:

A

if (condition1)
{
    if (condition2)
    {
        statement1;
    } else {
        statement2;
    }
}

B

if (condition1)
if (condition2)
statement1;
else statement2;

To the compiler, both pieces of code above will achieve the same result; however, the top block is far easier to read for us humans. Which code would you like to maintain.

Don’t use variable names that have no meaning – remember comments in the code should not be used to explain what your variables are, let their names tell all. This rule needs no code explanation, just stay away from one letter variables like a, b, or c and try to spell them out like

A

        double    a;
        double    b;
a = (b - 32) * 0.555555556

 

B

 

        double    celsius;
        double    fahrenheit;
celsius = (fahrenheit - 32) * 0.555555556

Bet you didn’t know that example A did until you got a look at B, right.

Test often and test early. How many lines do you write before you stop and see if what you have compiles. Do you write test cases for all your functions when you code them and actually test them before connecting them to the user interface? This helps you tackle bugs close to the source without wading through hundreds of lines of code.

If your IDE does not have a nice auto save feature like Jet Brain’s WebStorm, then please remember to save early and save often. There is nothing more frustrating than losing hours of good code.

Nobody ever wanted an app that ran slower, keep that in mind and optimize, optimize, optimize. You can use tools like Page Speed, or Chrome’s Developer Tools to get your JavaScript on track. In addition Google and Yahoo have written detailed articles that are essentially a lessons learned document