View Full Version : Uses vs requires

2 Mar 2012, 10:11 AM
I curious as to the difference between the uses property and the requires property. Looking through the source, they seem to be used interchangeably and very inconsistently. Any guidance on how and when to use each properly?


2 Mar 2012, 11:28 AM
The class(es) defined in the requires property will require them to be loaded before the class that is being defined is defined. The uses property doesn't guarantee those classes are loaded before anything even instantiated.

2 Mar 2012, 1:29 PM
From reading the docs, that is how I understood it as well. I think my confusion comes from how I see these used throughout the Ext code base. For example, look at Ext.util.Floating:

Ext.define('Ext.util.Floating', {
uses: ['Ext.Layer', 'Ext.window.Window'],
constructor: function(config) {
var me = this;

me.floating = true;
me.el = Ext.create('Ext.Layer', Ext.apply({}, config, {

In this case, it uses Ext.Layer, and in the constructor it creates an Ext.Layer object.

Now look at Ext.view.TableChunker:

Ext.define('Ext.view.TableChunker', {
singleton: true,
requires: ['Ext.XTemplate'],
constructor: function() {
Ext.XTemplate.prototype.recurse = function(values, reference) {
return this.apply(reference ? values[reference] : values);

In this case, it requires Ext.XTemplate, and immediately accesses it's prototype in the constructor.

Is the difference because one uses Ext.create, whereas the other goes right at the objects prototype?

And more generally, what is a situation where you cannot use uses and must use requires? And is there a situation where you must use uses and not requires? Or is this case more of a performance optimization?

2 Mar 2012, 2:21 PM
In this case, I believe the difference is that the TableChunker is a singleton, whereas Floating is not. Remember that for singletons, the constructor will execute right at class definition time - so any classes accessed in the constructor will need to be loaded before the class is defined and thus must be listed in "requires". For non-singleton classes, though, the constructor won't execute until an instance of the class is created, and the dependency can be listed in "uses" instead.

2 Mar 2012, 2:34 PM
Ok, I'm getting closer to fully wrapping my head around the difference. Let's look at a non-singleton class - Ext.AbstractManager:

Ext.define('Ext.AbstractManager', {
requires: ['Ext.util.HashMap'],
constructor: function(config) {
this.all = Ext.create('Ext.util.HashMap');

Is it correct to say that this object should really be using uses instead of requires?