Solved

ko is not defined when adding extension to Knockout

Posted on 2014-12-01
7
1,112 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:James Atkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:James Atkin
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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 4

Author Comment

by:James Atkin
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:James Atkin
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)
Suggested Courses

622 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