0

Estoy tratando de asociar una función de un ViewModel al click de un botón, pero KnockOut me da un error:

Unexpected token '}'

Lo más gracioso, es que la función no tiene código. Esta es la definición de la ventana (la parte que interese), hay otro botón que tiene asociada la función save del ViewModel y funciona correctamente.

<button data-bind="click: delete">Eliminar</button>

<script type="text/javascript">
    window.onload = function() {
      const ko = require("knockout");
      ...
      
      const service = new BranchService(httpRequester, "branches");
      //const branch = Branch.withDefaultValues();
      const branch = new Branch({
        id: 3,
        name: "name",
        description: "description",
        city: "city",
        address: "address"
      });
      const viewModel = new BranchViewModel(branch, service);

      ko.applyBindings(viewModel);
    }
</script>

Y esta es la definición del ViewModel. Tiene más código, pero no es relevante.

const ko = require("knockout");

const Branch = require("../core/models/branch");

class BranchViewModel {

  constructor(branch, branchService) {

    this._service = branchService;
    this.updating = ko.observable(false);
    this._startModel(branch);
  }

  _startModel(branch) {
    this.id = ko.observable(branch.id);
    this.name = ko.observable(branch.name);
    this.description = ko.observable(branch.description);
    this.city = ko.observable(branch.city);
    this.address = ko.observable(branch.address);
    this.isDeleted = ko.observable(branch.deleted);
    this.isNew = ko.observable(branch.isNew())
    this.canBeDeleted = ko.computed(
      function() {
        return !this.isDeleted() && !this.isNew();
      }, this);
  }

  _setModel(branch) {

    this.id(branch.id);
    this.name(branch.name);
    this.description(branch.description);
    this.city(branch.city);
    this.address(branch.address);
    this.isDeleted(branch.deleted);
    this.isNew(branch.isNew());
  }  

  save() {
    //tiene código
  }

  delete() {
    //sin código
  }
}

module.exports = BranchViewModel;

¿Alguien sabe por qué KnockOut me daría ese error?. Por lo que veo, el código no tiene errores.

1 respuesta 1

0

El problema es el nombre del operador delete.

Se trata de una palabra reservada en Javascript y no se puede usar en KnockOut.

https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Palabras_Reservadas

https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Operadores/delete

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.