-
11 May 2011 5:18 PM #1
What's the difference between 'requires' and 'uses' when defining a class?
What's the difference between 'requires' and 'uses' when defining a class?
What's the difference between 'requires' and 'uses' when defining a class? I can't find good documentation on this.
What's the advantage of using one over the other?
From a quick glance at the code, it appears the classes declared in the 'uses' array will still be loaded. So why not just put them in the 'requires' array?
-
11 May 2011 6:11 PM #2
From http://dev.sencha.com/deploy/ext-4.0...iew/index.html
Uses are optional class dependencies that are used by, but not required by, a class. These can be loaded asynchronously and do not have to be available for the class to be instantiated.
Not sure that helps any.
-
11 May 2011 6:25 PM #3
I must of missed that section in the overview. Thanks.
It helps a little. However, it seems that if a class uses another class, then won't it be required to function correctly?
Maybe it is used when a class may need another class but only if configured in a certain way. For example, class A depends on Class B only if a certain config option is set. Would we have to use Ext.require('ClassB'); if we set this config option or will Class B have already been loaded if it were declared in the 'uses' array?
-
12 May 2011 1:51 PM #4Sencha - Sencha Touch Dev Team
- Join Date
- Jul 2009
- Location
- Palo Alto, California
- Posts
- 469
- Vote Rating
- 9
It's easy to explain with an example:
Break-down of what would happen behind the scene:Code:Ext.define('A', { requires: ['B'], uses: ['C'] }, function() { // This callback function is invoked right when class A is created // Both A and B are ready here, but C is not });
- B is dynamically loaded if it doesn't yet exist
- A is created, the callback is executed
- C is loaded
The same applies with:
However:Code:Ext.require('A', function() { // Both A and B are ready here, but C is not });
Another purpose of 'uses' is to 'temporarily' solve deadlock situation. A bad class design would normally lead to a deadlock, for example:Code:Ext.require('A'); Ext.onReady(function(){ // A, B and C are all available });
- A extends B
- B mixins C
- C requires A
Sometimes refactoring the code base is not applicable due to certain objective / subjective reasons, and deadlock could be solved by deferring the loading priority of a certain class. The above example could be changed to:
- A extends B
- B mixins C
- C uses A
This occasionally happened when migrating dinosaur code to the new class system.Sencha Touch Lead Architect
-
12 May 2011 2:25 PM #5
so the conclusion sounds like: don't use "uses" regulary, it's just needed for some tenuous situations. Right?
vg Steffen
--------------------------------------
Release Manager of TYPO3 4.5
energlobe.de - german online magazine
-
12 May 2011 2:26 PM #6
Please add this explanation to the docs.
-
13 May 2011 12:35 AM #7
Is this available in Sencha Touch as well? I can't find Ext.define and Ext.require in Sencha Touch Api Documentation...
If not, is it ok to use Ext Core alongside Sencha Touch?
Similar Threads
-
Two ways of creating custom class in ExtJS: what's the difference?
By charris in forum Ext 3.x: Help & DiscussionReplies: 2Last Post: 7 Nov 2010, 11:48 PM -
Is there a difference in adding widgets in the main class or overridding onrender?
By TheBuzzer in forum Community DiscussionReplies: 0Last Post: 18 Dec 2009, 8:42 AM -
difference between xtype and custom class... why??
By tarini in forum Ext 2.x: Help & DiscussionReplies: 7Last Post: 21 Jan 2009, 5:13 AM -
Tip: About includes / requires
By achebv in forum Community DiscussionReplies: 10Last Post: 6 Oct 2008, 10:45 AM


Reply With Quote