我在尝试使用requireJS with knockout和knockout映射合并插件(而不是淘汰映射插件)时遇到了麻烦,请参阅此处的链接:https://github.com/grofit/knockout.mapping.merge/blob/master/src/knockout.mapping.merge.js
脚本:
requirejs.config({
paths: {
'knockout': 'scripts/knockout-3.0.0',
'ko-merge': 'scripts/knockout_mapping_merge'
},
shim: {
'knockout_mapping_merge': {
deps: ['knockout']
}
}
});
require(['knockout', 'ko-merge'], function (ko) {
var vm = function () {
var self = this;
self.forename = ko.observable("FName1");
self.surname = ko.observable("SName1");
self.merge = function () {
var x = {
forename: 'FName2',
surname: 'SName2'
};
ko.mapping.mergeFromJS(self, x);
};
};
ko.applyBindings(new vm());
});
我收到的错误是: SCRIPT5007:无法设置未定义或空引用的属性“mergeFromJS” 文件:knockout_mapping_merge.js,行:41,列:5
淘汰赛映射合并的声明如下:
(function(knockout){
...
})(typeof exports === 'undefined'? this['ko'] : require("knockout"));
我的理解是,这应该将淘汰称为依赖,但它似乎不起作用......任何想法为什么?
答案 0 :(得分:1)
您的问题是您在路径和shim配置中指定的模块名称是不同的。
requirejs.config({
paths: {
'knockout': 'scripts/knockout-3.0.0',
'ko-merge': 'scripts/knockout_mapping_merge'
},
shim: {
'knockout_mapping_merge': {
deps: ['knockout']
}
}
});
应该是
requirejs.config({
paths: {
'knockout': 'scripts/knockout-3.0.0',
'ko-merge': 'scripts/knockout_mapping_merge'
},
shim: {
'ko-merge': {
deps: ['knockout']
}
}
});
答案 1 :(得分:0)
插件中的那段代码正在寻找一个CommonJS场景(例如nodejs模块),而不是像requirejs这样的AMD场景。
尝试使用shimming Knockout导出ko
:
shim: {
'knockout': {
exports: 'ko'
},
// you should be using the module ID rather than the filename here
'ko-merge': {
deps: ['knockout']
}
}
答案 2 :(得分:0)
感谢您的回复,遗憾的是没有解决它,我最终更改了插件,因此它有了围绕它的定义来与requirjs一起工作:
define(['knockout'], function(knockout) {
knockout.mapping = {};
...
});