我有一个问题我试图解决2天而现在我只是放弃,所以这是我的最后一个资源,我正在尝试将JSON响应加载到Durandal中的observableArray(KO)但是每次我调用函数ko.mapping.fromJSON时都会出现以下错误:
Uncaught TypeError: Cannot call method 'fromJSON' of undefined
我知道我需要下载插件并将其加载到main.js
requirejs.config({
paths: {
'text': '../lib/require/text',
'durandal':'../lib/durandal/js',
'plugins' : '../lib/durandal/js/plugins',
'transitions' : '../lib/durandal/js/transitions',
'knockout': '../lib/knockout/knockout',
'mapping': '../lib/knockout/knockout.mapping',
'bootstrap': '../lib/bootstrap/js/bootstrap',
'jquery': '../lib/jquery/jquery-1.9.1'
},
urlArgs: 'v=1.0.0.1',
shim: {
'bootstrap': {
deps: ['jquery'],
exports: 'jQuery'
}
}
});
Knockout工作正常,我可以更新字段和所有内容,但是当我尝试使用ko.mapping时遇到问题:
define(['durandal/app', 'plugins/router', 'knockout', 'mapping'], function (app, router, ko) {
var clients = ko.observableArray([]);
return {
displayName: 'My Page',
clients: clients,
activate: function () {
return $.ajax({
type: "POST",
url: "backend/ServerServices.php",
data: {
s: 'listadodeclientes'
}
}).then(function( msg ) {
ko.mapping.fromJSON(msg, clients);
});
},
showMessage: function () {
router.navigate('crearcliente');
}
};
});
答案 0 :(得分:3)
为了在require.js中使用ko.mapping插件,你必须手动将ko.mapping变量设置为ko.mapping库或改为使用注入变量。
样品:
define(['durandal/app', 'plugins/router', 'knockout', 'mapping'], function (app, router, ko, mapping) {
ko.mapping = mapping;
ko.mapping.fromJSON(...
//or use injected mapping parameter
mapping.fromJson(...
}
查看this问题的答案。