I'm having an issue with ko.mapping plugin for knockout. I've been thoroughly searching for an answer but couldn't find anything.
I'm using knockout in conjunction with boilerplate js, but I think that's not the problem.
Here's the JS:
define(function(require){
var Boiler = require('Boiler'),
komapping = require('knockout_mapping');
ko.mapping = komapping;
var mapping = {
'observe': ['disciplina',
'numero',
'paraUsoEn',
'detalleCertificadoCalidad',
'comentariosGenerales']
};
var RequisicionViewModel = function(moduleContext, params, bindingCallback){
/* Propiedades del modelo */
var self = this;
this.disciplinas = ko.observableArray();
this.requisicion = ko.mapping.fromJS({});
/* Obtener los valores del WS */
// Obtener las disciplinas
moduleContext.requestor.get('/disciplina/').done(function(data){
self.disciplinas(data);
});
// Obtener la plantilla de la requisición
moduleContext.requestor.get('/requisicion/ZFN-5612').done(function(data){
ko.mapping.fromJS(data, mapping, self.requisicion);
self.requisicion.planos = ko.observable("Jola!")
// Aplicar el binding
bindingCallback();
});
/* Gestión de eventos */
this.onGuardarClicked = function(){
console.log(ko.mapping.toJSON(self.requisicion));
};
};
return RequisicionViewModel;
});
As you can see I define only the objects I want to be observable.
Here's the HTML
<div id="uso-planos-informacion" class="clearfix" data-bind="with:requisicion">
<div class="control-grp">
<label for="usarse-en" class="text-solid">{{nls.label_usarse_en}}</label>
<input id="usarse-en"
type="text"
data-bind="value:paraUsoEn">
</div>
<div class="control-grp">
<label for="planos" class="text-solid">{{nls.label_planos}}</label>
<input id="planos"
type="text">
</div>
<div class="control-grp">
<label for="certificado-calidad" class="text-solid">{{nls.certificado_calidad}}</label>
<input id="certificado-calidad"
type="text"
data-bind="value:detalleCertificadoCalidad">
</div>
</div><!-- Termina uso-planos-informacion -->
It's much longer, but for brevity I'll just paste 2 fields that show the error. Finally when I run it, this is what happens:
https://i.stack.imgur.com/2Vasm.png
Here's what I've tried so far:
- Use () after the variable name so that it evaluates and shows the value inside the observable.
This works, but the observable looses it's properties or something like that because it does not get updated again after this.
Defining a create function for the mapping. Al ready tried this:
var mapping = {'paraUsoEn':{create:function(options){return ko.observable(options.data);}}}
And does not work. The value does not appear, neither it can be updated.
Hope Someone has solved this kind of problem, otherwise I'll have to do the mapping manually (which works!).
Thanks!