Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

requirejs - Knockout.js mapping plugin with require.js

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...