Using Sencha Cmd with dynamically loaded controllers - extjs

I've created an application using Ext JS 4. controllers property in my app.js contains only the main controller:
Ext.application({
name: 'MyApp',
appFolder: 'app',
controllers: [
"main.App"
],
loadController: function(controller) {
var oController = this.getController(controller);
oController.init(this);
oController.onLaunch(this);
}
});
MyApp.main.App controller loads additional controllers by name using getController() approach (see loadController() method). These controllers are loaded dynamically and are not listed in my index.html file.
In order to generate production version for deployment to server I am using Sencha Cmd by issuing the following command in my application folder:
sencha app build
Tool finishes normally and compresses all files into one big all-classes.js. The problem is that my dynamically loaded controllers are not included in that file.
Which is the correct way to make dynamically loaded controllers (over 100 in total) to be minified and processed by Sencha Cmd?
I know, that I can list them in my app.js, or include in some file using Ext.require, but I am seeking for correct approach for including over than 100 different controllers, views, models and stores automatically in my build. I believe that are other users of Ext JS, which are creating large-scale applications and are building somehow and I'll be grateful for any suggestions or just success stories, which will help me to find the correct way to build.

I would put all controllers into the uses array. These should force the tool to keep track on them and include them into the build. On the other hand uses does not require the class to be available at definition time but guarantee them to be available the time the onReady(one is within the application) block(s) is(are) called.
Note that you will need to use the fully qualified names within the
uses array!
I do not use the buildtool therefore I cannot test it but it should work.
Update from the comments example provided by #bhovhannes
bhovhannes: I've added a code in build.xml, which collects all names of
my controllers into uses array when I do sencha app build. This way I
fill nothing in uses array during development, just add controllers
into controller folder, because all them are loaded dynamically from
my app
app.js
Ext.application({
name: 'MyApp',
appFolder: 'app',
controllers: [
"main.App"
],
uses: [
/*ant-generated-content-start*/ /*ant-generated-content-end*/
],
autoCreateViewport: true,
});
build.xml
<?xml version="1.0" encoding="utf-8"?>
<project name="MyApp" default=".help">
<import file="${basedir}/.sencha/app/build-impl.xml"/>
<target name="-before-build">
<echo message="Collecting all controllers in application class property ... "/>
<fileset id="app_controllers" dir="${app.dir}/app/controller" casesensitive="yes">
<include name="**/*.js"/>
</fileset>
<pathconvert pathsep="," property="app_controller_names" refid="app_controllers" targetos="unix">
<chainedmapper>
<globmapper from="${app.dir}/app/*" to="${ant.project.name}/*" casesensitive="no" handledirsep="yes"/>
<chainedmapper>
<regexpmapper from="^(.*)\.js$$" to='"\1"'/>
<filtermapper>
<replacestring from="/" to="."/>
<replacestring from="\" to="."/>
</filtermapper>
</chainedmapper>
</chainedmapper>
</pathconvert>
<echo message="Collected controllers: ${app_controller_names}"/>
<echo message="Injecting into app.js ..."/>
<replaceregexp file="${app.dir}/app/app.js"
match="/\*ant-generated-content-start\*/(.*)/\*ant-generated-content-end\*/"
replace="/*ant-generated-content-start*/ ${app_controller_names} /*ant-generated-content-end*/"
byline="true"
/>
</target>
<target name="-after-build">
<echo message="Reverting to original app.js ..."/>
<replaceregexp file="${app.dir}/app/app.js"
match="/\*ant-generated-content-start\*/(.*)/\*ant-generated-content-end\*/"
replace="/*ant-generated-content-start*/ /*ant-generated-content-end*/"
byline="true"
/>
</target>
</project>

Related

What does "packages" property of requireJs config mean ? And how it works?

I am trying to understand how requireJs works. There is a property "packages" which I came across while configuring requireJs. What my understanding is that 'packages' is used to mention a complete folder/module which contains "main.js" & main.js requires all other dependencies inside that module. But does mentioning "packages" in config file will automatically load main.js or do we need to do something to make it load main.js ?
Below is my folder structure & main.js snippet. 'main.js' is the data-main or entry point of application.
So after trying few stuff, what I understood is that packages lets you mention a directory or folder which has other modules (commonJs directory is what docs refer it to). So the way we define a package is :
packages: [
{name : 'controllers' , location :'../controllers' },
{name : 'directives' , location : '../directives'},
{name : 'services' , location : '../services'}
] ,
Where name is an alias for this entire directory or folder. location is the path of the folder relative to your main.js or requireJs's config file.
Now answering the 2) point - it does not load automatically. For it to load we need to require it somewhere. Once we require it, requireJs will first load the main.js inside that directory by default. And we need to define all the other modules inside that directory, in this main.js. For eg- I will require it in my app.js before bootstrapping my application-
require([
// Add additional dependencies
'angular',
'angular-ui-router',
'jquery',
'app',
'route',
"controllers",
"directives",
"services"] , function(){ console.log("All dependencies loaded"); });

Ext MVC does not load all folders and source files

I'm new to Ext and it's my first contact with this framework and I'm creating java web app. Here is my folder structure:
src
--main
--webapp
--index
--application
--controller
--Index.js
--model
--store
--view
--MainPanel.js
--Index.js
--resources
--WEB-INF
--Index.jsp
Here is webapp/Index/Index.js
Ext.application({
name: 'Spring_Ext',
appFolder: '/index/application',
controllers: [
'Index'
]
});
And here is webapp/index/application/controller/Index.js
Ext.define('Spring_Ext.controller.Index',{
extend: 'Ext.app.Controller',
views: ['MainPanel'],
init: function(){
....
}
....
});
When I run it on tomcat in chrome I get error saying it cannot found(404)
GET http://localhost:8081/index/application/controller/Index.js?_dc=1425849848988 ext-all-debug.js:6262
and when looking in source tab in chrome developer in index folder there isn't application folder with MVC structure, but only Index.js.
Thats because at the index.js level of your directory structure you are already in /index/application/
Extjs is now looking for the same path and cannot find it. Instead just specify '/' for you appFolder config. You might even not need to set this as it may default to this already
No need for appFolder. And in your source tab there is no application folder with mvc because your controller hasn't loaded successfully. Try giving the whole path of MainPanel in our controller like this
Views: Spring_Ext.view.MainPanel

How can I exclude files

Using CMD from within Sencha Architect I've been able to build a production build of my application. However I can not seem to figure out how to exclude a js file from the build process. I don't want it compiled in with app.js I want it as a separate script include in index.html - so cmd shouldn't touch it basically.
Sencha Arhitech generates and calls build.xml which calls build-impl.xml which calls init-impl.xml
Everywhere I've read, they say to include the following;
<target name="-before-init">
<echo>Setting build.operations...</echo>
<echo>app.dir=${app.dir}</echo>
<property name="build.operations">
exclude
-file=\resources\js\version.js
</property>
</target>
However it refuses to exclude the file...I can see the echos so I know it's hitting the target..
Any ideas? Is this how I am supposed to exclude files?
app.framework.version=4.2.1.883
app.cmd.version=4.0.4.84
Turns out this won't be possible to do until Sencha Architect 3.1
Steps by which i was able to exclude AppConfig file in production build.
Here file exclude means it will not be compressed/bundled and variable/properties of this file could be used any where in the app.
1. Config file(AppConfig.js in our case) MUST be inside resources fodler. Below are the contents of our AppConfig file
/////////////IxDetect is my Application Namespace///////////////////
var IxDetect = IxDetect || {};
IxDetect.AppConfig = {
logoPath: '',
logoTitle: 'Internal',
pentahoUrl: 'http://107.20.104.150/pentaho',
pentahoRptCube: 'TrafficWithFraudIndex'
};
////////////////////////////////
2. Link this file in index.html page like below
<script src="resources/AppConfig.js"></script>
3. Add one more item in "js" array in "app.json" file
"js": [
{
"path": "resources/AppConfig.js", // This is my file. Also make a sure you do not miss bundle and includeInBundle property
"bundle": false,
"includeInBundle": true
},
{
"path": "app.js",
"bundle": true
}
],
4. Try development and production build all should work file
Note: All above changes are done and tested on 6.2(Framework/CMD)

One Backbone.js app using require.js depends on another: how do I include the child?

I wrote a backbone.js app that uses require.js and is broken up with models/, collections/ and so forth. I then wrote another app that depends on the first app (and some other things. The files are laid out like so:
/scripts/appA/
models/
collections/
views/
/scripts/appNeedsA/
models/
collections/
views/
What do I put in the needsA to require appA? The below seems logical to me but doesn't work. If I use ../../appA, that finds appA but IT's dependencies can't be found because the root is wrong.
define(
['underscore', 'backbone', '../appA'],
function (_, Backbone, appA) {
...
}
It might not be the answer you were hoping for but, here's one approach:
https://github.com/busticated/RequireLoadingExample
The idea is that you define your module deps using the path the consuming application will use, then in the consumed app you alias the path appropriately.
In my example, I have a top-level main.js file which pulls in both app1.js and app2.js modules. Both of these modules depend on modules within their own sub-directories - e.g. app1.js uses one/mods/a.js and one/mods/b.js. I have another main (main-one.js) file that lives a level down inside the one/ directory. This file calls:
require.config({
paths: {
'jquery': 'libs/jquery',
'one': '.'
}
});
So now when app1.js loads, the one/mods/a.js path is translated to ./mods/a.js and is found / loaded without issue.
You should be able to fork my repo above and load index.html & one.html in a browser with js console open to see it all work.
Hope it helps!
The proper solution is to:
define(
['underscore', 'backbone', 'appA/views/whatever'],
function (_, Backbone, appAWhateverView) {
...
}
and to set your require.config paths to include:
require.config({
paths: {
appA: '../appA'
}
});

Jasmine require.js maven plugin

I am trying to configure jasmine maven plugin to load jasmine specifications using require.js.
Below is my maven configuration
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<specRunnerTemplate>REQUIRE_JS</specRunnerTemplate>
<jsSrcDir>${project.basedir}/src/main/webapp/backbone/js</jsSrcDir>
<jsTestSrcDir>${project.basedir}/src/test/javascript/spec</jsTestSrcDir>
<srcDirectoryName>js</srcDirectoryName>
<specDirectoryName>spec</specDirectoryName>
<specIncludes>
<include>*Spec.js</include>
</specIncludes>
<preloadSources>
<source>backbone/js/libs/jquery/jquery.js</source>
<source>backbone/js/libs/require/require.js</source>
<source>backbone/js/libs/underscore/underscore.js</source>
<source>backbone/js/libs/backbone/backbone.js</source>
<source>backbone/js/libs/bootstrap/bootstrap.js</source>
<source>backbone/js/config.js</source>
</preloadSources>
</configuration>
</plugin>
Below is my jasmine specification:
define(['models/Specification'], function(Specification) {
describe("Specification", function() {
var specification;
beforeEach(function() {
specification = new Specification();
})
it("thickness set to 4 by default", function () {
expect(specification.thickness).toEqual(4);
});
});
});
Below is the javascript file
define(["backbone"], function ($, _, Backbone) {
return Backbone.Model.extend({
defaults: {
thickness: 4
}
});
});
I get the following error : TypeError: Cannot call method "extend" of undefined
Is there anyway to load my require.js config instead of loading them manually ?
Any help is appreciated.
Thanks,
Ravi
To expand on rjz's post and to further answer the question...Yes, you can load your require.config file and let it load all of your library files/dependencies for you. There's no need to list the files in the plugin's configuration. I wrote a quick post summarizing my experience with jasmine-maven-plugin using Require.js jasmine+maven+requirejs+coverage. I included my pom.xml in my post for reference. Your main.js (or whatever includes your require.config) should just be one of your source files yes? So should point to your source directory, and thus your require.config gets loaded, and it handles the loading of your dependencies, not jasmine-maven-plugin. Hope my this helps.

Resources