Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8204471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:54:47+00:00 2026-06-07T07:54:47+00:00

I am reading through titanium best practises and i was wondering why this doesnt

  • 0

I am reading through titanium best practises and i was wondering why this doesnt appear to work can some one tell me whats changed with the api?

https://wiki.appcelerator.org/display/guides/Mobile+Best+Practices

ui/ToggleBox.js – A custom check box

   function ToggleBox(onChange) {
      this.view = Ti.UI.createView({backgroundColor:'red',height:50,width:50});

      //private instance variable
      var active = false;

      //public instance functions to update internal state, execute events
      this.setActive = function(_active) {
        active = _active;
        this.view.backgroundColor = (active) ? 'green' : 'red';
        onChange.call(instance);
      };

      this.isActive = function() {
        return active;
      };

      //set up behavior for component
      this.view.addEventListener('click', function() {
        this.setActive(!active);
      });
    }
    exports.ToggleBox = ToggleBox;

Sample usage in app.js

var win = Ti.UI.createWindow({backgroundColor:'white'});
var ToggleBox = require('ui/ToggleBox').ToggleBox;

var tb = new ToggleBox(function() {
  alert('The check box is currently: '+this.isActive());
});
tb.view.top = 50;
tb.view.left = 100;

win.add(tb.view);

it doesn’t seem to want to return the setActive method when called from the add event listener?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:54:48+00:00Added an answer on June 7, 2026 at 7:54 am

    The “this” in your click listener isn’t what you’re expecting it to be. (It’s probably the view.) Because your function is already the ToggleBox context, the easiest solution is to just use a direct reference to setActive. “this.” is only necessary for the API you’re exposing for other code.

    function ToggleBox(onChange) {
      var view = this.view = Ti.UI.createView({backgroundColor:'red',height:50,width:50});
    
      //private instance variable
      var active = false;
    
      //public instance functions to update internal state, execute events
      var setActive = this.setActive = function(_active) {
        active = _active;
        view.backgroundColor = (active) ? 'green' : 'red';
        onChange.call(instance);
      };
    
      var isActive = this.isActive = function() {
        return active;
      };
    
      //set up behavior for component
      view.addEventListener('click', function() {
        setActive(!active);
      });
    }
    exports.ToggleBox = ToggleBox;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading through this question on multi-threaded javascript, I was wondering if there would be
I an reading through some examples of normalization, however I have come across one
I was reading through older questions and this one made me wonder how you
I tried reading through some of the other questions but I still can't get
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
Reading through some of the questions here, the general concensus seems to be that
Was reading through some text and playing around with attempting to write past the
It's clear from reading through threads that I can call a PHP function using
I'm currently reading through this jquery masking plugin to try and understand how it
i've been reading through the linq to xml documentation in msdn and some other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.