Solved

ko is not defined when adding extension to Knockout

Posted on 2014-12-01
7
782 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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 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…

705 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

16 Experts available now in Live!

Get 1:1 Help Now