-
24 Feb 2012 2:39 AM #1
Best practices for global definitions in MVC pattern
Best practices for global definitions in MVC pattern
I am new to Sencha Touch and especially to the new loading class system... so sorry if my question is too silly.
I wonder what are the best practices for the definition of global data using the new class system, the MCV pattern and a custom build (using the SDK tools).
This is my example:
I want to define a set of XTemplates to use in several views. I don't want to define these XTemplates directly in each view (but in separate javascript files).
An option could be to define the various XTemplate as global variables, in this case do I have to explicitly load the js file with this definitions in the index page (using a script tag)? Or is there another way, using the Loader component?
Another option is to create a class and define the templates as static properties of this class. I.E.
(the file GeneralTemplates.js is in the app/templates folder)
Then in my view I can refer to a template like this:Code:Ext.define('MyApp.templates.GeneralTemplates', { requires: ['Ext.XTemplate'], statics:{ template1: new Ext.XTemplate( '<tpl for=".">', [...] '</tpl>' ), template2: new Ext.XTemplate( '<tpl for=".">', [...] '</tpl>' ) }, });
But in this case I have an error: "Cannot read property 'GeneralTemplates' of undefined"Code:Ext.define('MyApp.view.HomePanel' ,{ extend: 'Ext.Panel', requires: ['MyApp.templates.GeneralTemplates'], xtype : 'homepanel', config: { title: 'Home', items: [ { xtype: 'dataview', tpl: MyApp.templates.GeneralTemplates.template1 [...] } ], [...] }, [...] });
This error is thrown before the init methods of the controllers and the launch method of the application (and before the constructor of the view) are executed. If I log the same object in one of these methods, it is correctly printed!
What do you think guys? What is the best practice to define global variables observing the MVC pattern?
Sorry again if this question is too silly.
-
24 Feb 2012 9:07 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,656
- Vote Rating
- 435
I would create a manager of some sort:
Now you should be able to do:Code:Ext.define('MyApp.util.Templates', { singelton : true, config : { someTpl : '......' }, constructor : function(config) { this.initConfig(config); this.callParent([config]); } });
Code:MyApp.util.Templates.getSomeTpl()
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
15 May 2012 12:32 PM #3
I tried this method, but I got an error
MyApp.util.Templates.getTemplate() doesn't work!Code:Uncaught TypeError: Object function () { return this.constructor.apply(this, arguments); } has no method 'getTemplate'
-
15 May 2012 12:55 PM #4
Oh...
This works:
Code:var templates = new MyApp.util.Templates; vat tpl = templates.getTemplate();
-
15 May 2012 11:45 PM #5
Thanks for the tips. I did this:
1. I defined a Template class in a separate js File (app/templates/GeneralTemplates.js)
2. In the app.js i defined a configuration for the loader and the properly required property:PHP Code:Ext.define('Templates.GeneralTemplates', {
singelton : true,
statics:{
tpl1: new Ext.XTemplate( ... ),
tpl2: new Ext.XTemplate( ... )
}
constructor: function(config) {
this.initConfig(config);
this.callParent([config]);
}
}
3. I used the template this way:PHP Code:Ext.Loader.setConfig({
enabled: true,
paths : {
Templates : 'app/templates'
}
});
Ext.application({
name: 'MyApp',
requires: ['Templates.GeneralTemplates', ...],
...
})
PHP Code:Templates.GeleralTemplates.tpl1
-
16 May 2012 4:15 AM #6Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,656
- Vote Rating
- 435
Instead of statics, try config
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.


Reply With Quote