knockout.mapping & Require.js bug
Asked Answered
B

1

2

So I am trying to integrate knockout.mapping.js with require.js. I have tried following this example.

Unfortunately, I keep on getting this error:

GET http://[url]/Scripts/app/contractor/ko.js 404 (Not Found)
Uncaught Error: Script error for: ko

I have set up following files:

app.index.js

requirejs.config({
    "baseUrl": "../Scripts/app/contractor",
    "paths": {
        "app.index": "app.index",
        "knockout": "//cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min",
        "mapping": "//cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.3.5/knockout.mapping",
        "knockout.bindings": "../../lib/knockout.bindings",
        "fu": "../../lib/jquery.fineuploader-3.8.0",
        "s2": "../../lib/select2",
        "jquery": "//code.jquery.com/jquery-latest.min",
        "jqueryui": "//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min",
        "moment": "../../lib/moment.min",
        "toastr": "../../lib/toastr"
    },
    "shim": {
        'mapping': {
            deps: ['knockout'],
            exports: 'mapping'
        }
    }
});

// Load the main app module to start the app
require(['knockout', 'mapping', "main.index"], function (ko, mapping, bs) {
    ko.mapping = mapping;
    alert('ko: ' + ko + ', mapping: ' + ko.mapping);
    bs.run();
});

main.index.js

    define(['knockout',
            'mapping',
            'indexViewModel'],
       function (ko, mapping, indexViewModel) {
           var
               run = function () {

                   var vm = new indexViewModel();


                   ko.applyBindings(vm, document.getElementById('#contractor-home-view'));
               };
           return {
               run: run
           };
       });

main.index.js is irrelevant at this point because the mapping is not loading properly. What am I doing wrong here?

Bouilli answered 20/9, 2013 at 20:55 Comment(4)
There doesn't seem to be a "ko.js" file anywhere in your example. I can only assume that you have some other code that is trying to use a dependency called "ko" (rather than "knockout") that does not exist.Hildahildagard
I am loading knockout.js in the requirejs.config section. Funny thing is that when I make the config like this: "ko": "//cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min". It tells me that knockout.js is missing.Bouilli
sorry meant to say "it tells me that 'knockout' is missing"Bouilli
autogenerated.com/2012/09/…Bouilli
Q
0

Try making this in your .config

"shim": {
        'knockout': {
          exports: 'ko'
        },
        'mapping': {
          deps: ['knockout'],
          exports: 'mapping'
        }
    }
Quickel answered 23/1, 2014 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.