1. #1
    Sencha User
    Join Date
    Sep 2011
    Posts
    3
    Vote Rating
    0
    John Mark is on a distinguished road

      0  

    Default 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)

    Code:
    Ext.define('MyApp.templates.GeneralTemplates', {
        requires: ['Ext.XTemplate'],
        statics:{
          template1: new Ext.XTemplate(
             '<tpl for=".">', [...] '</tpl>'
          ),
          template2: new Ext.XTemplate(
             '<tpl for=".">', [...] '</tpl>'
          )
    
        },
    });
    Then in my view I can refer to a template like this:

    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
              [...]
            }
          ], [...]
        },    
       [...]
    
    });
    But in this case I have an error: "Cannot read property 'GeneralTemplates' of undefined"
    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.

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,791
    Vote Rating
    833
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    I would create a manager of some sort:

    Code:
    Ext.define('MyApp.util.Templates', {
        singelton : true,
    
        config : {
            someTpl : '......'
        },
    
        constructor : function(config) {
            this.initConfig(config);
            this.callParent([config]);
        }
    });
    Now you should be able to do:

    Code:
    MyApp.util.Templates.getSomeTpl()
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Apr 2012
    Posts
    17
    Vote Rating
    0
    zozo4kin is on a distinguished road

      0  

    Default


    I tried this method, but I got an error
    Code:
    Uncaught TypeError: Object function () {                    
    return this.constructor.apply(this, arguments);                
    } has no method 'getTemplate'
    
    MyApp.util.Templates.getTemplate() doesn't work!

  4. #4
    Sencha User
    Join Date
    Apr 2012
    Posts
    17
    Vote Rating
    0
    zozo4kin is on a distinguished road

      0  

    Default


    Oh...
    This works:
    Code:
    var templates = new MyApp.util.Templates;
    vat tpl = templates.getTemplate();

  5. #5
    Sencha User
    Join Date
    Sep 2011
    Posts
    3
    Vote Rating
    0
    John Mark is on a distinguished road

      0  

    Default


    Thanks for the tips. I did this:

    1. I defined a Template class in a separate js File (app/templates/GeneralTemplates.js)

    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]);
        }

    2. In the app.js i defined a configuration for the loader and the properly required property:

    PHP Code:
    Ext.Loader.setConfig({
        
    enabledtrue,
        
    paths   : {
            
    Templates 'app/templates'
        
    }
        
    });

    Ext.application({
        
    name'MyApp',
        
    requires: ['Templates.GeneralTemplates', ...],
        ...
    }) 
    3. I used the template this way:

    PHP Code:
    Templates.GeleralTemplates.tpl1 

  6. #6
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,791
    Vote Rating
    833
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Instead of statics, try config
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

    When posting code, please use BBCode's CODE tags.

  7. #7
    Sencha Premium Member
    Join Date
    Dec 2013
    Posts
    12
    Vote Rating
    0
    wellsfargouser is on a distinguished road

      0  

    Default Fiddle - to understand Statics, global variable declarations in MVC pattern.

    Fiddle - to understand Statics, global variable declarations in MVC pattern.


    Hi Friends,

    I wrote one simple example to understand Statics, global variable declarations in MVC pattern.

    Check this Fiddle.

    http://jsfiddle.net/prajavk/YhuWT/

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi