Making an Event

I'd like to reference a variable from my app class in my view class. The view needs to update as the variable updates. I think that the best way to do this would be to make an event, not quite sure how to do it with Garmin, can anyone help?

Thanks

  • If I understand, in your App base, when the variable changes, call WatchUi.requestUpdate()

  • I think that that's useful, but let me provide part of the view class. I create an App Base object, and on startup, the variable loads fine. But when the variable changes, the View class does not recognize it:

        hidden var myText;
    	
    	var app = new ODgarminUIprojApp();
    
        function initialize() {
            View.initialize();
        }
    
        // Load your resources here
        function onLayout(dc) {
            setLayout(Rez.Layouts.HomeLayout(dc));
            
        }
    
        // Called when this View is brought to the foreground. Restore
        // the state of this View and prepare it to be shown. This includes
        // loading resources into memory.
        function onShow() {
        	
        }
    
        // Update the view
        function onUpdate(dc) {
            // Call the parent onUpdate function to redraw the layout
            if (app.riskLevel == 1){
        		myText = new WatchUi.Text({
                	:text=>"1",
                	:color=>Graphics.COLOR_DK_GREEN,
                	:font=>Graphics.FONT_LARGE,
                	:locX =>WatchUi.LAYOUT_HALIGN_CENTER,
                	:locY=>WatchUi.LAYOUT_VALIGN_CENTER
            		});
            }
            else if (app.riskLevel == 2){
        		myText = new WatchUi.Text({
                	:text=>"2",
                	:color=>Graphics.COLOR_GREEN,
                	:font=>Graphics.FONT_LARGE,
                	:locX =>WatchUi.LAYOUT_HALIGN_CENTER,
                	:locY=>WatchUi.LAYOUT_VALIGN_CENTER
            		});
            }
            else if (app.riskLevel == 3){
        		myText = new WatchUi.Text({
                	:text=>"3",
                	:color=>Graphics.COLOR_YELLOW,
                	:font=>Graphics.FONT_LARGE,
                	:locX =>WatchUi.LAYOUT_HALIGN_CENTER,
                	:locY=>WatchUi.LAYOUT_VALIGN_CENTER
            		});
            }
            else if (app.riskLevel == 4){
        		myText = new WatchUi.Text({
                	:text=>"4",
                	:color=>Graphics.COLOR_RED,
                	:font=>Graphics.FONT_LARGE,
                	:locX =>WatchUi.LAYOUT_HALIGN_CENTER,
                	:locY=>WatchUi.LAYOUT_VALIGN_CENTER
            		});
            }
            else if (app.riskLevel == 5){
        		myText = new WatchUi.Text({
                	:text=>"5",
                	:color=>Graphics.COLOR_DK_RED,
                	:font=>Graphics.FONT_LARGE,
                	:locX =>WatchUi.LAYOUT_HALIGN_CENTER,
                	:locY=>WatchUi.LAYOUT_VALIGN_CENTER
            		});
            }
            View.onUpdate(dc);
            myText.draw(dc);
            //dc.clear();
        }

  •     var app = new ODgarminUIprojApp();

    ^ This is the problem.

    You're not supposed to instantiate the app object inside of the view class; the app object has already been instantiated as part of the app lifecycle. Also, the app instantiates the view and not the other way around.

    If you want the view to be able to see App members, pass in a reference to the app in the constructor.

    class YourView {
       
    var app;
        function 
    initialize(_app) {
            View.
    initialize();
            app = _app;
        }

    In the app:

    class YourApp extends Application.AppBase {
        
    // ...

        // Return the initial view of your application here
        function getInitialView() {
            
    return [ new YourView(self) ];
        }

        // ...
    }
  • Thanks for the help, this makes sense.

  • Doesn’t Application.getApp() return the app in any case? It’s what I’ve used in similar circs. 

  • Yeah, either way would work. Sorry for the overly-complicated suggestion.