Error in javascript backbone: A "url" property or function must be specified

1

I'm doing a design by mvc in JavaScript backbone, but when I implement the function to remove an element from the collection, I see this error:

A "url" property or function must be specified

This is the code:

eliminarCliente: function(idCliente) {

  var cliente = this.clientes.findWhere({      
    id: idCliente
  });

  if(cliente){
    cliente.destroy();
    this.clientes.remove(cliente);

    router.navigate('listarClientes', {
      trigger: true
    });

  }
}

Capture

    
asked by Vipefa 21.05.2017 в 06:25
source

1 answer

0

The problem is surely in this line:

cliente.destroy();

The method destroy what it does is, using Backbone.sync , try to destroy that model on the server by means of a request DELETE , returning the object XMLHttpRequest of that request or false if the object referenced by that model is not yet saved on the server.

If you are using a API Rest to update your data on the server, add the URL to the model so that Backbone knows where to update that data (in this parameter it is expected or the representation String of a URL or a function):

var miModelo = Backbone.Model.extend({
    url: "/url/a/la/api/rest/"
});

If on the other hand you are not using a API Rest and you are saving the data locally (using localStorage or sessionStorage ) without using an adapter such as Backbone.localStorage , then try to make an override parameter sync to rewrite each method CRUD with your own implementation:

  

If you read the entry of Backbone.sync , you can see each of the parameters that sync receives:

     
  • method: will be the one that contains the string representation of the CRUD method
  •   
  • model: will come the model you want to save, delete, update, etc ...
  •   
  • options: (optional parameter) is where the success and error callbacks will be contained
  •   
var miModelo = Backbone.Model.extend({

    sync: function(metodo, modelo, opciones) {

        switch (metodo) {

            case "create":
            break;

            case "read":
            break;

            case "update":
            break;

            case "delete":
            break;

        }

    }

});
    
answered by 21.05.2017 в 11:19