SlideShare una empresa de Scribd logo
1 de 46
Descargar para leer sin conexión
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:

Más contenido relacionado

Destacado

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
 

Destacado (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
 

Más de 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
 

Más de 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
 

Último

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
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
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
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
%+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
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
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
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 

Último (20)

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
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
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...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
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...
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%+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...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
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
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 

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: