Reputation: 168
I'm having a problem I've trying to solve for 2 days and now I just give up, so this is my last resource, I'm trying to load a JSON response into an observableArray (KO) in Durandal but everytime I call the function ko.mapping.fromJSON is comes with the following error:
Uncaught TypeError: Cannot call method 'fromJSON' of undefined
I understand I need to download the plugin and load it on the 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 is working fine, I'm able to update fields and everything, but when I try to use the ko.mapping I'm having problems:
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');
}
};
});
Upvotes: 3
Views: 2074
Reputation: 141
In order to use ko.mapping plugin with require.js you have to manualy set ko.mapping variable to ko.mapping library or use injected variable instead.
Sample:
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(...
}
Look at the answer on this question.
Upvotes: 3