Solved

ko is not defined when adding extension to Knockout

Posted on 2014-12-01
7
946 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 43

Expert Comment

by:Rob
ID: 40476931
Do you still plan to use the pauseable library?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 43

Expert Comment

by:Rob
ID: 40481645
I'll take a look.
0
 
LVL 43

Accepted Solution

by:
Rob 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

820 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