Solved

ko is not defined when adding extension to Knockout

Posted on 2014-12-01
7
860 Views
Last Modified: 2014-12-07
Hi all,

I am trying to ass an extension to Knockout (knockout.pausable), but I get the error:

Uncaught ReferenceError: ko is not defined

I am using the requirejs to control the dependancies, but there seems to be a problem:

requirejs.config({
    shim: {
        'Lib/jquery-2.1.1': { exports: '$' },
        'Lib/knockout-3.2.0': { exports: 'ko', deps: ['Lib/jquery-2.1.1'] },
        'Lib/knockout-pausable-1.1.1': { deps: ['Lib/knockout-3.2.0'] }
    }
});

Open in new window


and then in the specific js file I want to use it, I have

require(["Lib/jquery-2.1.1","Lib/knockout-3.2.0","Lib/knockout-pausable-1.1.1"], function ($, ko, kopause) {
    . . . . 
});

Open in new window


Any ideas what I have done wrong?

Any help would be appreciated.

Thanks a lot,

James
0
Comment
Question by:jatkin
  • 3
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40473591
check your console to see if you don't have any 404 errors and double check your filenames
0
 
LVL 4

Author Comment

by:jatkin
ID: 40473626
No 404 errors and filenames all look okay.
the Knockout is used within the JS file and works okay.
It is only when it processes the extension file that is does not like 'ko' references.
Error within Chrome
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40476931
Do you still plan to use the pauseable library?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 4

Author Comment

by:jatkin
ID: 40478090
This is very true now, thanks to your help on my other question :-)

It would still be nice to know exactly what I have missed, as I'm sure I will use extensions to knockout in the future.
When I copy the library code into the 'code.js' file that uses it, it executes correctly.
I'm sure it is something silly that I have omitted to do, but I was not able to find it.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40481645
I'll take a look.
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40481789
seems a hack but you can attach the ko variable to the window once the knockout library has loaded:

<!DOCTYPE html>
<html>
    <head>
        <script src="Lib/require.js"></script>
        <script>
            require.config({
                baseUrl: 'Lib'                
            });
            require(["jquery-2.1.1.min", "knockout-min"],
                    function ($, ko) {
                        //This function will be called when all the dependencies
                        //listed above are loaded. Note that this function could
                        //be called before the page is loaded.
                        //This callback is optional.
                        window.ko = ko;
                        require(["main.js", "knockout.pauseable"]);
                    }
            );
        </script>
        <meta charset="utf-8">
        <title>JS Bin</title>
    </head>
    <body>

    </body>
</html>

Open in new window

0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 40486269
If I run into this problem next time I use an extension, I will use your suggestion.
Thanks a lot for all the help...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sums of coloumns in html/java 15 64
Javascript closure 1 34
Download a website to hdd 2 48
Replace &lt; with < 14 54
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now