Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ko is not defined with requirejs

I'm trying to use requirejs to load knockoutjs in following way:

<script type="text/javascript">
    require(['scripts/knockout-2.2.1.debug'], function() {
        var someModel = {
            firstname: ko.observable('asd')
        };
        ko.applyBindings(someModel);
    });
</script>

Where require.js is loaded using a Script Tag on top. When this code executes, I get a ko is not defined error. What's the correct way to do similar?

like image 789
helloworld Avatar asked Mar 03 '13 07:03

helloworld


1 Answers

require(['scripts/knockout-2.2.1.debug'], function(ko) {

I'm not sure but seeing the example on requirejs, I guess these are loaded inside the scope of that callback function. So, unless you accept that in your function, you can't use it.

Yes, you are right (at least, seeing their exmaple). if you include 4 libraries, you need to accept 4 parameters. for example, if you loaded jquery and knockout, you can write it as follows:

require(['scripts/jquery.js', 'scripts/knockout-2.2.1.debug'], function($, ko) {
like image 115
HungryCoder Avatar answered Sep 22 '22 22:09

HungryCoder