Problems using the Knockout.js template plugin with require

Showing 1-4 of 4 messages
Problems using the Knockout.js template plugin with require wataka 6/11/12 11:59 PM
am trying to modularize my code using require.js. I have a module
deifined as follows:

define(["assets/js/lodash.min.js", "assets/js/knockout-2.1.0.js",
"assets/js/koExternalTemplateEngine.min.js", "be-api"], function (_,
ko, te, api) {

This, does not work because the templating engine plugin for
Knockout.js assumes and emits an error stating that Knockout is
undefined. Can someone tell me what I am doing wrong?

Thanks
Re: [requirejs] Problems using the Knockout.js template plugin with require Miller Medeiros 6/12/12 5:45 AM
If "koExternalTemplateEngine" depends on "knockout" you need to use the
`shim` config or wrap the "koExternalTemplateEngine" into a `define` call,
AMD is asynchronous so "koExternalTemplateEngine" might execute before
"knockout" finishes loading, that's why it's a good practice to list all
the dependencies on all files.

Also remove the ".js" from the module IDs.. Modules with ".js" are treated
as regular scripts so RequireJS doesn't wait for a `define` call.

More info about the shim config:
http://requirejs.org/docs/api.html#config-shim
Re: Problems using the Knockout.js template plugin with require wataka 6/14/12 2:33 AM
Thanks for your reply Miller, but I am still having problems, this is
my config file:

require.config({
    baseUrl: "be",
    paths: {
        "vmtest": "viewmodels/vm-testl",
        "knockout": "lib/knockout",
        "kote": "lib/koExternalTemplateEnginel.min.js"
    },
    shim: {
        'kote': {
            deps: ['knockout']
        },
    }

  });

The shim does not work, and koExternalTemplateEngine_all.min.js still
errors out with the statement that "ko" is not defined

 shim: {
        'kote': { <-- Should this be the mapping name or the real name
of the file as in koExternalTemplateEngine_all.min.js?
            deps: ['knockout']  <-- Should this be the mapping name or
the real name of the file as in knockout.js?
        },
    }

Thanks
Re: Problems using the Knockout.js template plugin with require Alex Vasilenko 6/14/12 7:40 AM
Hello Wataka,

Shim won't help, because knockout supports AMD out of the box, but plugins are hardcoded to depend on 'knockout' module. It means that if you don't have 'knockout.js' right at baseUrl it throughs exception. Solution is to use path aliases. For your example:
require.config({ 
    baseUrl: "be", 
    paths: { 
        "vmtest": "viewmodels/vm-testl", 
        "knockout": "assets/js/knockout-2.1.0",
        "kote": "lib/koExternalTemplateEnginel.min" 
    }
});

Means that knockout is located at assets/js/knockout-2.1.0.js file, relative to baseUrl. Please notice, that you don't need to put '.js' in the end of dependencies, as requirejs adds it.

Regards,