SlideShare a Scribd company logo
1 of 46
Download to read offline
Titanium Beginning Best Practices
A little about me.
Josh Jensen
mashstack.com
Beginning Best Practices&
Goal: To help you keep your WTF/min to a minimum.
Beginning Best Practices&
Best Practices > General Javascript Topics
We all know Javascript has
it’s quirks, but it is a
powerful, expressive
language that allows us to
do amazing things.
A couple of great
books to read.
Beginning Best Practices&
Best Practices > General Javascript Topics
Beginning Best Practices&
Best Practices > General Javascript Topics
Reminder: !
Many of these are personal
preferences.
JSHint
Beginning Best Practices&
Best Practices > General Javascript Topics
http://www.jshint.com/
My preferred style guide
Beginning Best Practices&
Best Practices > General Javascript Topics
https://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
Ti. vs. Titanium. namespace
Beginning Best Practices&
Best Practices > General Titanium Topics
Remember: When typing out the namespace, if you
snicker you have gone too far, remove the “t”…
var  win  =  Ti.UI.createWindow({});  
    
//  vs.    
    
var  win  =  Titanium.UI.createWindow({});
Opening a Window
Beginning Best Practices&
Best Practices > General Titanium Topics
Avoid using the “url” property when creating a window.
//  No,  no,  no…  
var  win  =  Ti.UI.createWindow({  
   url:  “windows/window.js”  
});  
    
//  Yes  
var  win  =  Titanium.UI.createWindow({});
A Classic Folder Structure
Beginning Best Practices&
Best Practices > General Titanium Topics
Your folder structure needs to make as much sense as your
code. Remember that you are doing this for future you or
someone else.
-­‐ Resources  
-­‐ android  
-­‐ app.js  
-­‐ application  
-­‐ application.js  
-­‐ ui  
-­‐ helpers  
-­‐ lib  
-­‐ iphone  
!
!
Create an
“application”
folder for all
of your
Titanium
code.
Windows vs. Views
Beginning Best Practices&
Best Practices > General Titanium Topics
It’s up to you! Kind of.
CommonJS
Beginning Best Practices&
Bringing sanity to your project.
Best Practices > CommonJS
What is CommonJS?
Beginning Best Practices&
A simple API for declaring modules.
Best Practices > CommonJS
Some things to know about
CommonJS
Beginning Best Practices&
Best Practices > CommonJS
What does a CommonJS module
look like?
Beginning Best Practices&
Best Practices > CommonJS
//  Private  
var  count  =  0;  
    
//  Public  
exports.incrementCount  =  function()  {  
    count  =  count  +  1;  
    return  count;  
};
What does a CommonJS module look like?
Beginning Best Practices&
Best Practices > CommonJS
//  require  
var  libA  =  require("lib/liba");  
    
//  exports  
exports.createInstance  =  function()  {  
};
exports. vs. module.exports
Beginning Best Practices&
Best Practices > CommonJS
//  module.exports  
var  instanceFactory  =  {};  
instanceFactory.createInstance  =  function()  {  
};  
    
module.exports  =  instanceFactory;
exports.createInstance  =  function()  {  
};
Simple CommonJS App Example
Beginning Best Practices&
Best Practices > CommonJS
var  APP  =  require("application/application");  
APP.init();
var  APP  =  {};  
!
APP.init  =  function()  {  
   var  win  =  Ti.UI.createWindow({  
      backgroundColor:  "#fff"  
   });  
!
   var  label  =  Ti.UI.createLabel({  
      color:  "#333",  
      text:  "Hello  TiConf",  
      font:  {  
         fontSize:20,  
         fontFamily:  "Helvetica  Neue"  
      },  
      textAlign:  "center",  
      width:  "auto"  
   });  
   win.add(label);  
!
   win.open();  
};  
!
module.exports  =  APP;
app.js
application/application.js
Best Practices > CommonJS
var  APP  =  require("application/application");  
APP.init();
var  social  =  require("dk.napp.social");  
!
var  uiHelper  =  require("application/helpers/ui");  
!
var  APP  =  {};  
!
APP.init  =  function()  {  
   var  win  =  uiHelper.windowFactory({  
      backgroundColor:  "#fff"  
   },  {  
      onClose:  function()  {  
         win  =  null;  
         label  =  null;  
      }  
   });  
!
   var  label  =  uiHelper.labelFactory({  
      text:  "Hello  TiConf"  
   });  
   win.add(label);  
!
   win.open();  
};  
!
module.exports  =  APP;
app.js
application/application.js
var  _  =  require("application/vendor/underscore");  
!
exports.windowFactory  =  function(_params,  _listeners)  {  
        var  win  =  Ti.UI.createWindow(_.extend(_params,  {}));  
!
        var  onWindowClose  =  function()  {  
                Ti.API.info("Window  Closed");  
                if  (_listeners.onClose)  {  
                        _listeners.onClose();  
                }  
                win  =  null;  
                onWindowClose  =  null;  
        };  
!
        win.addEventListener("close",  onWindowClose);  
        return  win;  
};  
!
exports.labelFactory  =  function(_params)  {  
        var  label  =  Ti.UI.createLabel(_.extend(_params,  {  
                color:  "#333",  
                font:  {  
                        fontSize:20,  
                        fontFamily:  "Helvetica  Neue"  
                },  
                textAlign:  "center",  
                width:  "auto"  
        }));  
!
        return  label;  
};
application/helpers/ui.js
CommonJS can be used to share
data in the application.
Beginning Best Practices&
Best Practices > CommonJS
Using Native Modules
Beginning Best Practices&
Best Practices > CommonJS
var  social  =  require("dk.napp.social");
app.js
        <modules>  
                <module  platform="iphone">dk.napp.social</module>  
        </modules>
tiapp.xml
$  gittio  install  dk.napp.social
[sudo]  npm  install  -­‐g  gittio
Check out http://gitt.io
Formatting style preference
Beginning Best Practices&
Best Practices > CommonJS
var  social  =  require("dk.napp.social");  
!
var  uiHelper  =  require("application/helpers/ui");  
!
var  APP  =  {};
application/application.js
Note: This is a style preference… Not law.
Installed Modules
(Referenced in tiapp.xml)
Local CommonJS Modules
Declared Variables
Memory management
Beginning Best Practices&
Best Practices > Memory Management
Memory Management
Beginning Best Practices&
Best Practices > Memory Management
Titanium Native
Kroll Bridge
Windows, Buttons, Views, etc.
Memory Management
Beginning Best Practices&
Best Practices > Memory Management
exports.windowFactory  =  function(_params,  _listeners)  {  
        var  win  =  Ti.UI.createWindow(_.extend(_params,  {}));  
!
        var  onWindowClose  =  function()  {  
                Ti.API.info("Window  Closed");  
                if  (_listeners.onClose)  {  
                        _listeners.onClose();  
                }  
                win  =  null;  
                onWindowClose  =  null;  
        };  
!
        win.addEventListener("close",  onWindowClose);  
        return  win;  
};
Remember: If in doubt,
null it out…
        var  win  =  uiHelper.windowFactory({  
                backgroundColor:  "#fff"  
        },  {  
                onClose:  function()  {  
                        win  =  null;  
                        label  =  null;  
                }  
        });
application/application.js application/helpers/ui.js
Beginning Best Practices&
Best Practices > Memory Management
for  (var  i  =  0;  i  <  5;  i++)  {  
        var  star  =  Ti.UI.createImageView({  
                height:'44dp',  
                width:'44dp',  
                top:  "50dp",  
                left:'10dp',  
                backgroundColor:  "#333"  
        });  
          
        (function()  {  
                var  index  =  i;  
                star.addEventListener('click',  function()  {  
                        setRating(index+1);  
                });  
        })();  
          
        myView.add(star);  
}
Possible memory leaks
http://www.tidev.io/2014/03/27/memory-management/
Beginning Best Practices&
Best Practices > Memory Management         var  starWrapper  =  Ti.UI.createView({  
                layout:  "horizontal",  
                top:  "10dp",  
                width:  "145dp",  
                height:  "24dp"  
        });  
!
        win.add(starWrapper);  
!
        for  (var  i  =  0;  i  <  6;  i++)  {  
                starWrapper.add(Ti.UI.createImageView({  
                        id:  "star"  +  i,  
                        height:  "24dp",  
                        width:  "24dp",  
                        left:  "5dp",  
                        image:  "/images/star.png",  
                        opacity:  0.5  
                }));  
        }  
!
        var  onStarTap  =  function(e)  {  
                _.each(starWrapper.getChildren(),  function(child)  {  
                        child.setOpacity(0.5);  
                });  
                e.source.setOpacity(1);  
                setRating(e.source.id);  
                Ti.API.info(currentRating);  
        };  
!
        starWrapper.addEventListener("click",  onStarTap);
A better way…
No reference to createImage has
been stored.
We are not adding an eventListener
to each imageView
Beginning Best Practices&
Best Practices > Memory Management
//  Global  system  Events  
Ti.Network.addEventListener("change",  APP.networkObserver);  
Ti.Gesture.addEventListener("orientationchange",  APP.orientationObserver);  
Ti.App.addEventListener("pause",  APP.exitObserver);  
Ti.App.addEventListener("close",  APP.exitObserver);  
Ti.App.addEventListener("resumed",  APP.resumeObserver);  
!
if(OS_ANDROID)  {  
   APP.MainWindow.addEventListener("androidback",  APP.backButtonObserver);  
}
Global Event Listeners
Checking your memory management
Beginning Best Practices&
Best Practices > Memory Management
$  titanium  build  -­‐-­‐platform  ios  -­‐-­‐target  simulator  -­‐-­‐sim-­‐type  iphone  -­‐-­‐tall  -­‐-­‐retina
Build your project for the simulator
1
Go to your project folder > build >
iPhone > Click on the Xcode project
file.
2
Checking your memory management
Beginning Best Practices&
Best Practices > Memory Management
Open Xcode:
Select: Product > Destination > iPhone Retina (4-inch)
Then select an OS version, like 7.1

3
Checking your memory management
Beginning Best Practices&
Best Practices > Memory Management
Then select: Product > Profile4
Checking your memory management
Beginning Best Practices&
Best Practices > Memory Management
Wait…5
Checking your memory management
Beginning Best Practices&
Best Practices > Memory Management
Select: Allocations6
Checking your memory management
Beginning Best Practices&
Best Practices > Memory Management
Add “TiUI” here7
As you use your app in the
simulator you will see TiUI items
start showing up in your
Allocation summary.
8
Checking your memory management
Beginning Best Practices&
Best Practices > Memory Management
Add “TiUI” here
Checking your memory management
Beginning Best Practices&
Best Practices > Memory Management
Add “TiUI” here
Memory Management
Beginning Best Practices&
Best Practices > Memory Management
http://www.tidev.io/2014/03/27/memory-management/
Beginning Best Practices&
Best Practices > Alloy
Alloy
Highly recommended!
Beginning Best Practices&
Best Practices > Alloy
Alloy is a framework that
follows an MVC architecture …
What’s that?
model-view-controller (MVC)
Beginning Best Practices&
Best Practices > Alloy
Alloy specific best
practices.I have 3…
Beginning Best Practices&
Best Practices > Alloy
Alloy specific best
practices.
Use it!
Beginning Best Practices&
Best Practices > Alloy
Alloy specific best
practices.
Use Alloy.Global sparingly…
Use it, it’s incredibly powerful, but use it carefully.
Beginning Best Practices&
Best Practices > Alloy
Alloy specific best
practices.
Don’t forget to call $.destroy(); when you
are done with a controller.
$.win.addEventListener("close",  function(){  
        $.destroy();  
}  
Being a good community
member
Beginning Best Practices&
Best Practices > Community
http://bit.ly/appc-qa - Using
Questions and Answers
Read This
Then this
Finally, read this before you
post something…
Questions?
Beginning Best Practices&
Best Practices > General Titanium Topics
http://bit.ly/ticonfjosh
@joshj
Slides/Example app:
Twitter:

More Related Content

Viewers also liked

copper based shape memory alloys
copper based shape memory alloys copper based shape memory alloys
copper based shape memory alloys aseel safee
 
Patent Landscape Report on Shape Memory Material – Polymer and Alloy
Patent Landscape Report on Shape Memory Material – Polymer and AlloyPatent Landscape Report on Shape Memory Material – Polymer and Alloy
Patent Landscape Report on Shape Memory Material – Polymer and AlloyGridlogics
 
Nickel titanium in orthodontics /certified fixed orthodontic courses by India...
Nickel titanium in orthodontics /certified fixed orthodontic courses by India...Nickel titanium in orthodontics /certified fixed orthodontic courses by India...
Nickel titanium in orthodontics /certified fixed orthodontic courses by India...Indian dental academy
 
Differential scanning calorimetry [dsc]
Differential scanning calorimetry [dsc]Differential scanning calorimetry [dsc]
Differential scanning calorimetry [dsc]Sagar Savale
 
Shape memory alloy (ni tinol)
Shape memory alloy (ni tinol)Shape memory alloy (ni tinol)
Shape memory alloy (ni tinol)sandeshdhurve
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 80869840596838
 
Shape Memory Alloy Module
Shape Memory Alloy ModuleShape Memory Alloy Module
Shape Memory Alloy ModuleAccessNano
 
Shape memory alloys
Shape memory alloysShape memory alloys
Shape memory alloysEldho Peter
 

Viewers also liked (20)

Shape memory alloys
Shape memory alloysShape memory alloys
Shape memory alloys
 
copper based shape memory alloys
copper based shape memory alloys copper based shape memory alloys
copper based shape memory alloys
 
Niti (2)
Niti (2)Niti (2)
Niti (2)
 
Shape Memory Alloys (SMAs)
Shape Memory Alloys (SMAs)Shape Memory Alloys (SMAs)
Shape Memory Alloys (SMAs)
 
Smart Structures
Smart StructuresSmart Structures
Smart Structures
 
Niti
NitiNiti
Niti
 
Shape memory-materials
Shape memory-materialsShape memory-materials
Shape memory-materials
 
Shape memory-alloy
Shape memory-alloyShape memory-alloy
Shape memory-alloy
 
Patent Landscape Report on Shape Memory Material – Polymer and Alloy
Patent Landscape Report on Shape Memory Material – Polymer and AlloyPatent Landscape Report on Shape Memory Material – Polymer and Alloy
Patent Landscape Report on Shape Memory Material – Polymer and Alloy
 
Nickel titanium alloys
Nickel titanium alloysNickel titanium alloys
Nickel titanium alloys
 
Ni ti alloy
Ni ti alloyNi ti alloy
Ni ti alloy
 
Nickel titanium in orthodontics /certified fixed orthodontic courses by India...
Nickel titanium in orthodontics /certified fixed orthodontic courses by India...Nickel titanium in orthodontics /certified fixed orthodontic courses by India...
Nickel titanium in orthodontics /certified fixed orthodontic courses by India...
 
Shape memory alloys
Shape memory alloysShape memory alloys
Shape memory alloys
 
shape memory alloys
shape memory alloysshape memory alloys
shape memory alloys
 
Differential scanning calorimetry [dsc]
Differential scanning calorimetry [dsc]Differential scanning calorimetry [dsc]
Differential scanning calorimetry [dsc]
 
Shape memory alloy (ni tinol)
Shape memory alloy (ni tinol)Shape memory alloy (ni tinol)
Shape memory alloy (ni tinol)
 
Flow measurement part III
Flow measurement   part IIIFlow measurement   part III
Flow measurement part III
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Shape Memory Alloy Module
Shape Memory Alloy ModuleShape Memory Alloy Module
Shape Memory Alloy Module
 
Shape memory alloys
Shape memory alloysShape memory alloys
Shape memory alloys
 

More from joshcjensen

DevNexus 2018 - You Don't Know Angular
DevNexus 2018 - You Don't Know AngularDevNexus 2018 - You Don't Know Angular
DevNexus 2018 - You Don't Know Angularjoshcjensen
 
DevNexus 2018 - You don't know Angular
DevNexus 2018 - You don't know AngularDevNexus 2018 - You don't know Angular
DevNexus 2018 - You don't know Angularjoshcjensen
 
ConnectTech2017 - You don't know Angular
ConnectTech2017 - You don't know AngularConnectTech2017 - You don't know Angular
ConnectTech2017 - You don't know Angularjoshcjensen
 
Connect.js - Exploring React.Native
Connect.js - Exploring React.NativeConnect.js - Exploring React.Native
Connect.js - Exploring React.Nativejoshcjensen
 
Connect.js 2015 - Building Native Mobile Applications with Javascript
Connect.js 2015 - Building Native Mobile Applications with JavascriptConnect.js 2015 - Building Native Mobile Applications with Javascript
Connect.js 2015 - Building Native Mobile Applications with Javascriptjoshcjensen
 
Tying it all together in real time - Connect.JS / Ti.Connect
Tying it all together in real time - Connect.JS / Ti.ConnectTying it all together in real time - Connect.JS / Ti.Connect
Tying it all together in real time - Connect.JS / Ti.Connectjoshcjensen
 

More from joshcjensen (6)

DevNexus 2018 - You Don't Know Angular
DevNexus 2018 - You Don't Know AngularDevNexus 2018 - You Don't Know Angular
DevNexus 2018 - You Don't Know Angular
 
DevNexus 2018 - You don't know Angular
DevNexus 2018 - You don't know AngularDevNexus 2018 - You don't know Angular
DevNexus 2018 - You don't know Angular
 
ConnectTech2017 - You don't know Angular
ConnectTech2017 - You don't know AngularConnectTech2017 - You don't know Angular
ConnectTech2017 - You don't know Angular
 
Connect.js - Exploring React.Native
Connect.js - Exploring React.NativeConnect.js - Exploring React.Native
Connect.js - Exploring React.Native
 
Connect.js 2015 - Building Native Mobile Applications with Javascript
Connect.js 2015 - Building Native Mobile Applications with JavascriptConnect.js 2015 - Building Native Mobile Applications with Javascript
Connect.js 2015 - Building Native Mobile Applications with Javascript
 
Tying it all together in real time - Connect.JS / Ti.Connect
Tying it all together in real time - Connect.JS / Ti.ConnectTying it all together in real time - Connect.JS / Ti.Connect
Tying it all together in real time - Connect.JS / Ti.Connect
 

Recently uploaded

WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 

Recently uploaded (20)

WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

TiConf 2014 - Titanium Best Practices, memory management

  • 2. A little about me. Josh Jensen mashstack.com
  • 3. Beginning Best Practices& Goal: To help you keep your WTF/min to a minimum.
  • 4. Beginning Best Practices& Best Practices > General Javascript Topics We all know Javascript has it’s quirks, but it is a powerful, expressive language that allows us to do amazing things.
  • 5. A couple of great books to read. Beginning Best Practices& Best Practices > General Javascript Topics
  • 6. Beginning Best Practices& Best Practices > General Javascript Topics Reminder: ! Many of these are personal preferences.
  • 7. JSHint Beginning Best Practices& Best Practices > General Javascript Topics http://www.jshint.com/
  • 8. My preferred style guide Beginning Best Practices& Best Practices > General Javascript Topics https://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
  • 9. Ti. vs. Titanium. namespace Beginning Best Practices& Best Practices > General Titanium Topics Remember: When typing out the namespace, if you snicker you have gone too far, remove the “t”… var  win  =  Ti.UI.createWindow({});       //  vs.         var  win  =  Titanium.UI.createWindow({});
  • 10. Opening a Window Beginning Best Practices& Best Practices > General Titanium Topics Avoid using the “url” property when creating a window. //  No,  no,  no…   var  win  =  Ti.UI.createWindow({     url:  “windows/window.js”   });       //  Yes   var  win  =  Titanium.UI.createWindow({});
  • 11. A Classic Folder Structure Beginning Best Practices& Best Practices > General Titanium Topics Your folder structure needs to make as much sense as your code. Remember that you are doing this for future you or someone else. -­‐ Resources   -­‐ android   -­‐ app.js   -­‐ application   -­‐ application.js   -­‐ ui   -­‐ helpers   -­‐ lib   -­‐ iphone   ! ! Create an “application” folder for all of your Titanium code.
  • 12. Windows vs. Views Beginning Best Practices& Best Practices > General Titanium Topics It’s up to you! Kind of.
  • 13. CommonJS Beginning Best Practices& Bringing sanity to your project. Best Practices > CommonJS
  • 14. What is CommonJS? Beginning Best Practices& A simple API for declaring modules. Best Practices > CommonJS
  • 15. Some things to know about CommonJS Beginning Best Practices& Best Practices > CommonJS
  • 16. What does a CommonJS module look like? Beginning Best Practices& Best Practices > CommonJS //  Private   var  count  =  0;       //  Public   exports.incrementCount  =  function()  {      count  =  count  +  1;      return  count;   };
  • 17. What does a CommonJS module look like? Beginning Best Practices& Best Practices > CommonJS //  require   var  libA  =  require("lib/liba");       //  exports   exports.createInstance  =  function()  {   };
  • 18. exports. vs. module.exports Beginning Best Practices& Best Practices > CommonJS //  module.exports   var  instanceFactory  =  {};   instanceFactory.createInstance  =  function()  {   };       module.exports  =  instanceFactory; exports.createInstance  =  function()  {   };
  • 19. Simple CommonJS App Example Beginning Best Practices& Best Practices > CommonJS var  APP  =  require("application/application");   APP.init(); var  APP  =  {};   ! APP.init  =  function()  {     var  win  =  Ti.UI.createWindow({       backgroundColor:  "#fff"     });   !   var  label  =  Ti.UI.createLabel({       color:  "#333",       text:  "Hello  TiConf",       font:  {         fontSize:20,         fontFamily:  "Helvetica  Neue"       },       textAlign:  "center",       width:  "auto"     });     win.add(label);   !   win.open();   };   ! module.exports  =  APP; app.js application/application.js
  • 20. Best Practices > CommonJS var  APP  =  require("application/application");   APP.init(); var  social  =  require("dk.napp.social");   ! var  uiHelper  =  require("application/helpers/ui");   ! var  APP  =  {};   ! APP.init  =  function()  {     var  win  =  uiHelper.windowFactory({       backgroundColor:  "#fff"     },  {       onClose:  function()  {         win  =  null;         label  =  null;       }     });   !   var  label  =  uiHelper.labelFactory({       text:  "Hello  TiConf"     });     win.add(label);   !   win.open();   };   ! module.exports  =  APP; app.js application/application.js var  _  =  require("application/vendor/underscore");   ! exports.windowFactory  =  function(_params,  _listeners)  {          var  win  =  Ti.UI.createWindow(_.extend(_params,  {}));   !        var  onWindowClose  =  function()  {                  Ti.API.info("Window  Closed");                  if  (_listeners.onClose)  {                          _listeners.onClose();                  }                  win  =  null;                  onWindowClose  =  null;          };   !        win.addEventListener("close",  onWindowClose);          return  win;   };   ! exports.labelFactory  =  function(_params)  {          var  label  =  Ti.UI.createLabel(_.extend(_params,  {                  color:  "#333",                  font:  {                          fontSize:20,                          fontFamily:  "Helvetica  Neue"                  },                  textAlign:  "center",                  width:  "auto"          }));   !        return  label;   }; application/helpers/ui.js
  • 21. CommonJS can be used to share data in the application. Beginning Best Practices& Best Practices > CommonJS
  • 22. Using Native Modules Beginning Best Practices& Best Practices > CommonJS var  social  =  require("dk.napp.social"); app.js        <modules>                  <module  platform="iphone">dk.napp.social</module>          </modules> tiapp.xml $  gittio  install  dk.napp.social [sudo]  npm  install  -­‐g  gittio Check out http://gitt.io
  • 23. Formatting style preference Beginning Best Practices& Best Practices > CommonJS var  social  =  require("dk.napp.social");   ! var  uiHelper  =  require("application/helpers/ui");   ! var  APP  =  {}; application/application.js Note: This is a style preference… Not law. Installed Modules (Referenced in tiapp.xml) Local CommonJS Modules Declared Variables
  • 24. Memory management Beginning Best Practices& Best Practices > Memory Management
  • 25. Memory Management Beginning Best Practices& Best Practices > Memory Management Titanium Native Kroll Bridge Windows, Buttons, Views, etc.
  • 26. Memory Management Beginning Best Practices& Best Practices > Memory Management exports.windowFactory  =  function(_params,  _listeners)  {          var  win  =  Ti.UI.createWindow(_.extend(_params,  {}));   !        var  onWindowClose  =  function()  {                  Ti.API.info("Window  Closed");                  if  (_listeners.onClose)  {                          _listeners.onClose();                  }                  win  =  null;                  onWindowClose  =  null;          };   !        win.addEventListener("close",  onWindowClose);          return  win;   }; Remember: If in doubt, null it out…        var  win  =  uiHelper.windowFactory({                  backgroundColor:  "#fff"          },  {                  onClose:  function()  {                          win  =  null;                          label  =  null;                  }          }); application/application.js application/helpers/ui.js
  • 27. Beginning Best Practices& Best Practices > Memory Management for  (var  i  =  0;  i  <  5;  i++)  {          var  star  =  Ti.UI.createImageView({                  height:'44dp',                  width:'44dp',                  top:  "50dp",                  left:'10dp',                  backgroundColor:  "#333"          });                    (function()  {                  var  index  =  i;                  star.addEventListener('click',  function()  {                          setRating(index+1);                  });          })();                    myView.add(star);   } Possible memory leaks http://www.tidev.io/2014/03/27/memory-management/
  • 28. Beginning Best Practices& Best Practices > Memory Management        var  starWrapper  =  Ti.UI.createView({                  layout:  "horizontal",                  top:  "10dp",                  width:  "145dp",                  height:  "24dp"          });   !        win.add(starWrapper);   !        for  (var  i  =  0;  i  <  6;  i++)  {                  starWrapper.add(Ti.UI.createImageView({                          id:  "star"  +  i,                          height:  "24dp",                          width:  "24dp",                          left:  "5dp",                          image:  "/images/star.png",                          opacity:  0.5                  }));          }   !        var  onStarTap  =  function(e)  {                  _.each(starWrapper.getChildren(),  function(child)  {                          child.setOpacity(0.5);                  });                  e.source.setOpacity(1);                  setRating(e.source.id);                  Ti.API.info(currentRating);          };   !        starWrapper.addEventListener("click",  onStarTap); A better way… No reference to createImage has been stored. We are not adding an eventListener to each imageView
  • 29. Beginning Best Practices& Best Practices > Memory Management //  Global  system  Events   Ti.Network.addEventListener("change",  APP.networkObserver);   Ti.Gesture.addEventListener("orientationchange",  APP.orientationObserver);   Ti.App.addEventListener("pause",  APP.exitObserver);   Ti.App.addEventListener("close",  APP.exitObserver);   Ti.App.addEventListener("resumed",  APP.resumeObserver);   ! if(OS_ANDROID)  {     APP.MainWindow.addEventListener("androidback",  APP.backButtonObserver);   } Global Event Listeners
  • 30. Checking your memory management Beginning Best Practices& Best Practices > Memory Management $  titanium  build  -­‐-­‐platform  ios  -­‐-­‐target  simulator  -­‐-­‐sim-­‐type  iphone  -­‐-­‐tall  -­‐-­‐retina Build your project for the simulator 1 Go to your project folder > build > iPhone > Click on the Xcode project file. 2
  • 31. Checking your memory management Beginning Best Practices& Best Practices > Memory Management Open Xcode: Select: Product > Destination > iPhone Retina (4-inch) Then select an OS version, like 7.1
 3
  • 32. Checking your memory management Beginning Best Practices& Best Practices > Memory Management Then select: Product > Profile4
  • 33. Checking your memory management Beginning Best Practices& Best Practices > Memory Management Wait…5
  • 34. Checking your memory management Beginning Best Practices& Best Practices > Memory Management Select: Allocations6
  • 35. Checking your memory management Beginning Best Practices& Best Practices > Memory Management Add “TiUI” here7 As you use your app in the simulator you will see TiUI items start showing up in your Allocation summary. 8
  • 36. Checking your memory management Beginning Best Practices& Best Practices > Memory Management Add “TiUI” here
  • 37. Checking your memory management Beginning Best Practices& Best Practices > Memory Management Add “TiUI” here
  • 38. Memory Management Beginning Best Practices& Best Practices > Memory Management http://www.tidev.io/2014/03/27/memory-management/
  • 39. Beginning Best Practices& Best Practices > Alloy Alloy Highly recommended!
  • 40. Beginning Best Practices& Best Practices > Alloy Alloy is a framework that follows an MVC architecture … What’s that? model-view-controller (MVC)
  • 41. Beginning Best Practices& Best Practices > Alloy Alloy specific best practices.I have 3…
  • 42. Beginning Best Practices& Best Practices > Alloy Alloy specific best practices. Use it!
  • 43. Beginning Best Practices& Best Practices > Alloy Alloy specific best practices. Use Alloy.Global sparingly… Use it, it’s incredibly powerful, but use it carefully.
  • 44. Beginning Best Practices& Best Practices > Alloy Alloy specific best practices. Don’t forget to call $.destroy(); when you are done with a controller. $.win.addEventListener("close",  function(){          $.destroy();   }  
  • 45. Being a good community member Beginning Best Practices& Best Practices > Community http://bit.ly/appc-qa - Using Questions and Answers Read This Then this Finally, read this before you post something…
  • 46. Questions? Beginning Best Practices& Best Practices > General Titanium Topics http://bit.ly/ticonfjosh @joshj Slides/Example app: Twitter: