Knockout.js mapping plugin with require.js

流过昼夜 提交于 2019-11-28 07:33:33

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.

Your configuration object can specify a set of dependencies and a callback where further dependency configuration/manipulation can be performed:

var require = {
  paths: {
    'knockout': '...',
    'mapping': '...'
  },

  // configuration dependencies

  deps: ['knockout', 'mapping'],

  // configuration callback

  callback: function (ko, mapping) {
    ko.mapping = mapping;
  }
};

And when you include your scripts in markup, config should be loaded before require:

<script src="/scripts/config.js" />
<script src="/scripts/require.js" />

Now, Knockout.js will be available with a ko.mapping property as desired when used in a module definition.

define(['knockout'], function (ko) {
  // ko.mapping is available
});

Please note that this is an abbreviated configuration example. There's some shimming needed for the mapping plugin to specify the proper exports, just don't remember off the top of my head what it is.

On a side note, because you are using Lo-Dash you don't need to include "underscore" in the RequireJS "shim" options. Lo-Dash has AMD support baked in.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!