×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    9

    Javascript 'return' in QML

    Howdy,

    I have this tiny, little problem that I just can't get working. Here's the situation.

    I'm not able to return the value from the Javascript file into the QML. Here's what I have at the moment - and I just can't get it work:

    Code:
    // MainPage.qml
    
    import QtQuick 1.1
    import com.nokia.meego 1.1
    
    import "Database.js" as Database
    
    Page
    {
        // ... some other things here!
    
        Component.onCompleted: console.log("In QML: " + Database._Load_Function())
    }
    Code:
    // Database.js
    
    function _Load_Function ()
    {
        // openDatabaseSync(a, b, c, d).transaction(
    
        function (tx)
        {
            // tx.executeSql(x)
    
            console.log("In Javascript: " + _ReturnValue)
            return _ReturnValue
        }
    
        )
    }
    The console outputs the following:

    Code:
    In Javascript: Hello world!
    In QML: undefined
    Any advice would be much appreciated!

    Sincerely,
    Heikki Kullas
    Last edited by hkullas; 2012-06-07 at 06:46.

  2. #2
    Super Contributor
    Join Date
    Jun 2006
    Location
    India
    Posts
    3,037

    Re: Javascript 'return' in QML

    In you _Load_Function (), I found symbol ) which is not required.
    Use [URL="http://qt.nokia.com/products/qt-quick/"]Qt-Quick[/URL] to make your application UI more attractive.

    [URL="http://store.ovi.com/content/271896"]http://store.ovi.com/content/271896[/URL] | [URL="http://store.ovi.com/content/276199"]http://store.ovi.com/content/276199[/URL] |[URL="http://store.ovi.com/content/276202"] http://store.ovi.com/content/276202[/URL] | [URL="http://store.ovi.com/content/280827"]http://store.ovi.com/content/280827[/URL]

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    48

    Re: Javascript 'return' in QML

    In your JS _Load_Function() does not return a value:
    _ReturnValue is returned from lambda to _Load_Function, but _Load_Function does not return it further up

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    9

    Re: Javascript 'return' in QML

    Quote Originally Posted by kkrish View Post
    In you _Load_Function (), I found symbol ) which is not required.
    Mmh, actually, that symbol closes the "openDatabaseSync().transaction(" function. So that won't be a problem.

    Quote Originally Posted by conxt View Post
    In your JS _Load_Function() does not return a value:
    _ReturnValue is returned from lambda to _Load_Function, but _Load_Function does not return it further up
    Okay, I think I got your point. How do I get that _ReturnValue then in my QML (through Javascript or .. ?).

  5. #5
    Super Contributor
    Join Date
    Jun 2006
    Location
    India
    Posts
    3,037

    Re: Javascript 'return' in QML

    The way you are using it is correct, I think you have some problem with you function.
    Below code is working fine for me:

    function _Load_Function ()
    {
    return 2;
    }

    Component.onCompleted: {
    console.log("Component.onCompleted : "+ _Load_Function())
    }

    Output:
    Component.onCompleted : 2

    If you are trying to return an object, which is also fine but you can not print an object.
    Use [URL="http://qt.nokia.com/products/qt-quick/"]Qt-Quick[/URL] to make your application UI more attractive.

    [URL="http://store.ovi.com/content/271896"]http://store.ovi.com/content/271896[/URL] | [URL="http://store.ovi.com/content/276199"]http://store.ovi.com/content/276199[/URL] |[URL="http://store.ovi.com/content/276202"] http://store.ovi.com/content/276202[/URL] | [URL="http://store.ovi.com/content/280827"]http://store.ovi.com/content/280827[/URL]

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    9

    Re: Javascript 'return' in QML

    Well, I've found that I can not return a value from the function inside another function.

    Code:
    function _Function_01 ()
    {
        function _Function_02 ()
        {
            return 2;
        }
    }
    
    Component.onCompleted: console.log("Component.onCompleted : " + _Function_01())
    I'm trying to solve how I'm able to return the value from a function which is inside another function.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    48

    Re: Javascript 'return' in QML

    The correct way to return is
    Code:
    function _Function_01 ()
    {
        _Function_02 = function ()
        {
            return 2;
        }
        return _Function_02 ();
    }

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    9

    Re: Javascript 'return' in QML

    Okay, that works -- almost.

    If I use it only that way, no problem. But what about in this situation:

    Code:
    // File: "MainPage.qml"
    
    import "Database.js" as Database;
    
    console.log(Database._Load_ColorTheme());
    
    // ----------
    
    // File: "Database.js"
    
    function _Load_ColorTheme ()
    {
        openDatabaseSync("ColorThemeDb", "1.0", "Color Theme", 1000000).transaction(
    
            tx_Function = function (tx)
            {
                tx.executeSql('CREATE TABLE IF NOT EXISTS Themes (ColorString TEXT)');
    
                var _Theme = tx.executeSql('SELECT * FROM Themes');
                var _ColorTheme = "";
                var _Color = "";
    
                for (var i = 0; i < _Theme.rows.length; i++)
                {
                    _ColorTheme = _Theme.rows.item(i).ColorString;
                }
    
                if (!_ColorTheme) _Color = "#09C";
                else _Color = _ColorTheme;
    
                return _Color;
             }
        )
    
        return tx_Function ();
    }
    It gives me this following error: Error: Invalid write to global property "tx_Function"

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    9

    Re: Javascript 'return' in QML

    Ah, never mind - found the solution that helped.

    Code:
    // File: "Database.js"
    
    var _Color = "";
    
    function _Load_ColorTheme ()
    {
        openDatabaseSync("ColorThemeDb", "1.0", "Color Theme", 1000000).transaction(
    
            function (tx)
            {
                tx.executeSql('CREATE TABLE IF NOT EXISTS Themes (ColorString TEXT)');
    
                var _Theme = tx.executeSql('SELECT * FROM Themes');
                var _ColorTheme = "";
                var _Color = "";
    
                for (var i = 0; i < _Theme.rows.length; i++)
                {
                    _ColorTheme = _Theme.rows.item(i).ColorString;
                }
    
                if (!_ColorTheme) _Color = "#09C";
                else _Color = _ColorTheme;
             }
        )
    
        return _Color;
    }
    And in QML just call the function and voilá !

Similar Threads

  1. Need help with javascript
    By ldenjs1313 in forum [Archived] Qt Quick
    Replies: 3
    Last Post: 2012-04-06, 06:56
  2. javascript app
    By jcharth in forum Mobile Java Tools & SDKs
    Replies: 1
    Last Post: 2010-11-18, 08:14
  3. javascript
    By Alfonso in forum Browsing and Mark-ups
    Replies: 1
    Last Post: 2003-03-19, 10:26
  4. Javascript and WML
    By Nokia_Archive in forum Browsing and Mark-ups
    Replies: 1
    Last Post: 2002-05-13, 19:54

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×