[SOLVED]"TypeError: Object [object Object] has no method" when using function from imported Javascript file



  • This is quite frustrating, because from what I understand this should work just fine. So, here's a little example of what I'm doing:
    my Qml file:
    @
    import "js/functions.js" as Funct

    // This item is only used to run javascript
    Item {
    Component.onCompleted: Funct.myFunction();
    }
    @

    my functions.js file:
    @
    function myFunction() {
    //Do stuff here
    }
    @

    But when I run it, it throws the error: TypeError: Object [object Object] has no method 'myFunction'

    If anyone can think of anything that could possibly cause something so simple to not work, please do tell.



  • It certainly should work.

    Actually, this is unit tested quite extensively in the qqmlecmascript unit test (https://qt.gitorious.org/qt/qtdeclarative/trees/stable/tests/auto/qml/qqmlecmascript/data/jsimport) so if it doesn't work for you, it's probably something related to the form of your import (importing a js resource directly via a relative path should be supported, but perhaps something got broken recently?)

    Can you try different forms of import:

    import "./js/functions.js" as Funct
    import "/absolute/path/to/js/functions.js" as Funct

    just to narrow down what the issue might be?

    Cheers,
    Chris.



  • Well it looks like my last post was deleted when the server went down last night, so let me sum it up. The problem was my javascript was originally in my QML file, in 'Component.onCompleted: {CODE WAS HERE}', in my javascript code I had a multiline quote such as this:
    @
    Qt.createQmlObject('
    DYNAMICALLY GENERATED QML GOES HERE
    ', parent, myObject);
    @

    When I copied and pasted the javascript into it's own file I needed to escape the literal newlines such as this:
    @
    Qt.createQmlObject('
    DYNAMICALLY GENERATED QML GOES HERE
    ', parent, myObject);
    @

    And that sums it up - _ -


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.