Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Posts: 133
Registered: ‎10-01-2012
My Device: Playbook
My Carrier: CMCC
Accepted Solution

javascript with QML

Hi,

I am facing difficulty while trying to integrate an existing javascript with my project.

 

My javascript code (alarm.js) is-

function alarm() {
    var time="12:00 am";
    getTimes: function (){
        return time;
    }
    setTimes: function (time){
        this.time=time;
    }
}

My QML code (main.qml) is -

import "alarm.js" as Alarm
Container {
                id: root
Label{
text:"Calling alarm:"
} function callAlarm(){ var times = Alarm.setTimes("06:00 am"); return Alarm.getAlarm(); } Label{ text: ""+callAlarm(); } }

 

 I can see "calling alarm" in the output, but not the next label which is supposed to be "06:00 am". What is going wrong? 

Developer
Posts: 828
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: javascript with QML

try removing ""+ from 

 

text: ""+callAlarm();

 if that doesn't do the trick giving label an id might help

 

import "alarm.js" as Alarm
Container {
                id: root
                Label{
                    id: alarmLabel
                    text:"Calling alarm:"
                }
                function callAlarm(){
                    var times = Alarm.setTimes("06:00 am");
                    return Alarm.getAlarm();
                }
                Label{
                text: alarmLabel.callAlarm();
                }
}

 

 

New Developer
Posts: 133
Registered: ‎10-01-2012
My Device: Playbook
My Carrier: CMCC

Re: javascript with QML

[ Edited ]

Still not working. I put another function with simple string return and it works. So, it seems the problem is in calling the javascript inside the callAlarm() function.


import "alarm.js" as Alarm
Container {
	id: root
	Label{
		text:"Calling alarm:"
	}
	
	function returnMe(){return "Return Me";}

	function callAlarm(){
		var times = Alarm.setTimes("06:00 am");
		return Alarm.getAlarm();
	}

	Label{
		text:root.returnMe();
		}

	Label{
		text: root.callAlarm();
	}
}

 

Developer
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: javascript with QML

[ Edited ]

Is it your typo? Alarm.getAlarm() is undefined.




p(^_^)q
Good good study, day day up
Developer
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: javascript with QML

Your javascript seems like not correct. You should write it as

 

var alarm =  {
    time : "12:00 am",
    getTimes: function (){
        return this.time;
    },
    setTimes: function (time){
        this.time=time;
    }
}

 

then use Alarm.alarm.getTimes or Alarm.alarm.setTimes




p(^_^)q
Good good study, day day up
Highlighted
New Developer
Posts: 133
Registered: ‎10-01-2012
My Device: Playbook
My Carrier: CMCC

Re: javascript with QML

Thanks a bunch! I completely forgot the function alarm() in my javascript file.