Global variable sencha touch 2.1 -
hi need define global variable use in anywhere of application. declare global variable baseurl in app.js. please see below app.js
//<debug> ext.loader.setpath({ 'ext': 'touch/src',//location of sencha touch source files 'bluebutton': 'app', }); //</debug> ext.application({ name: 'bluebutton',//application path, classes in app. eg bluebutton.view.main.case sensitive views: ['main', 'bluebutton.couponmain', 'bluebutton.couponlist', 'bluebutton.couponlist2', 'bluebutton.couponlist3', 'bluebutton.transactionmain', ], stores : [ 'bluebutton.globalvariable', ], models : ['bluebutton.globalvariable', 'bluebutton.memberdetail', ], controllers: ['main', 'bluebutton.memberlist', ], requires: [ 'ext.messagebox', ], icon: { '57': 'resources/icons/icon.png', '72': 'resources/icons/icon~ipad.png', '114': 'resources/icons/icon@2x.png', '144': 'resources/icons/icon~ipad@2x.png' }, isiconprecomposed: true, startupimage: { '320x460': 'resources/startup/320x460.jpg', '640x920': 'resources/startup/640x920.png', '768x1004': 'resources/startup/768x1004.png', '748x1024': 'resources/startup/748x1024.png', '1536x2008': 'resources/startup/1536x2008.png', '1496x2048': 'resources/startup/1496x2048.png' }, //--global value-- baseurl: 'http://192.168.251.108:8080', //--global value-- launch: function() { // destroy #apploadingindicator element ext.fly('apploadingindicator').destroy(); // initialize main view var loginls = ext.getstore('loginls'); loginls.load(); var record = loginls.getat(0); if(record != undefined){ var sessionid = record.get('sessionid'); if (sessionid !=undefined){ var mainview = ext.getcmp("mainview"); if(!mainview){ mainview = ext.create('bluebutton.view.main'); } ext.viewport.setactiveitem(mainview); } else { var loginview = ext.getcmp("loginview"); if(!loginview){ loginview = ext.create('bluebutton.view.login'); } ext.viewport.setactiveitem(loginview); } } else{ var loginview = ext.getcmp("loginview"); if(!loginview){ loginview = ext.create('bluebutton.view.login'); } ext.viewport.setactiveitem(loginview); // //--disable line -- // var mainview = ext.getcmp("mainview"); // if(!mainview){ // mainview = ext.create('bluebutton.view.main'); // } // ext.viewport.setactiveitem(mainview); // //--disable line -- } // ext.create('bluebutton.view.topmenulist'); }, init: function () { this.callparent(arguments); }, onupdated: function() { ext.msg.confirm( "application update", "this application has been updated latest version. reload now?", function(buttonid) { if (buttonid === 'yes') { window.location.reload(); } } ); } });
model.js
ext.define('bluebutton.model.bluebutton.couponlist', { extend: 'ext.data.model', config: { idproperty: 'couponid', fields: [ { name: 'couponid' }, { name: 'couponname' }, { name: 'description' }, { name: 'amount' }, { name: 'coupontype' }, { name: 'merchant_bbid' }, { name: 'sessionid' }, { name: 'deviceid' }, ], proxy: { type: 'rest', url: bluebutton.app.baseurl +'/webcommon/rest/bbwebservice/getcouponlist', actionmethods: { create: 'post', read: 'get', update: 'put', destroy: 'delete' }, nocache: false, // rid of '_dc' url parameter extraparams: { sessionid: "1", merchant_bbid: "merchant1", }, // timeout:1000, // listeners: { // exception: function(proxy, response, operation) { // alert("connection problem"); // ext.viewport.setmasked(false); // hide load screen // // } // }, reader: { type: 'json', rootproperty: 'couponlist' }, writer: { type: 'json', }, } } });
then used basedurl in model.js. can work when use browser view. when use sencha app build testing compile apps,
i used browser open , showed me error message uncaught typeerror: cannot read property 'baseurl' of undefined . idea?
when make production build, files in sencha app minified , global variables may lose context.
there several ways declare global variables in sencha app
-> 1st approach
declare global variables in util/config.js
util/config.js
ext.define('app.util.config', { singleton : true, alias : 'widget.appconfigutil', config : { baseurl : 'xx.xx.xx.xxx', }, constructor: function(config) { this.initconfig(config); this.callparent([config]); } })
changes in app.js
requires : [ 'app.util.config']
now, can use in application below.
var baseurl = app.util.config.getbaseurl();
2nd approach->
declare global variables in .js files before class definition
var baseurl; ext.define('classname,{other things });
Comments
Post a Comment