Knockout.js mapping plugin with require.js

Ravi picture Ravi · Jun 25, 2012 · Viewed 7.9k times · Source

What is the standard way of loading mapping plugin in require.js ?

Below is my config.js (require.js config file)

require.config({
    // Initialize the application with the main application file.
    deps: ["app"],

    paths:{
        // JavaScript folders.
        libs: "lib",
        plugins: "lib/plugin",
        templates: "../templates",

        // Libraries.
        jquery: "lib/jquery-1.7.2.min",
        underscore: "lib/lodash",
        text: 'text',
        order: 'order',
        knockout: "lib/knockout",
        knockoutmapping: "lib/plugin/knockout-mapping"

    },

    shim:{
        underscore:{
            exports: '_'
        },

        knockout:{
            deps: ["jquery"],
            exports: "knockout"
        }
    }
}

In my view model

define(['knockout', 'knockoutmapping'], function(ko, mapping) {
}

However, mapping is not bound to ko.mapping. Any pointers/suggestions would be appreciated.

Thanks, Ravi

Answer

RP Niemeyer picture RP Niemeyer · Jun 26, 2012

When used with AMD, the mapping plugin exports its functionality into a separate object. So, the functionality will be attached to your mapping variable and you would call methods off of it (like mapping.fromJS).

You could choose to set ko.mapping equal to mapping in your code, if you have code that relies on ko.mapping that you are unable to change.