Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ko is not defined when adding extension to Knockout

Posted on 2014-12-01
7
Medium Priority
?
1,285 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
  • 3
  • 3
7 Comments
 
LVL 83

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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

824 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