Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED]"TypeError: Object [object Object] has no method" when using function from imported Javascript file
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tory Gaurnier
    wrote on 28 May 2013, 22:09 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on 29 May 2013, 00:28 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tory Gaurnier
        wrote on 29 May 2013, 18:08 last edited by
        #3

        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 - _ -

        1 Reply Last reply
        0

        1/3

        28 May 2013, 22:09

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved