3

I have this function:

function db_borrarServer(idABorrar){
    serversDB.servidores
        .filter(function(elementoEncontrado) {
            return elementoEncontrado.id_local == this.idABorrar; 
        })
        .forEach(function(elementoEncontrado){
                console.log('Starting to remove ' + elementoEncontrado.nombre);
                serversDB.servidores.remove(elementoEncontrado);
                serversDB.saveChanges();
        });    
}

does not work, but it does if I replace the variable "this.idABorrar" with a number, it does

return elementoEncontrado.id_local == 3; 

or if I declare idABorrar as a global, works to.

I need to pass idABorrar as variable. How can I do this?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
CRIMUVI
  • 121
  • 1
  • 10

3 Answers3

2

The EntitySet filter() function (as any other predicate functions) are not real closure blocks, rather an expression tree written as a function. To resolve variables in this scope you can only rely on the Global and the this which represents the param context. This follows HTML5 Array.filter syntax. To access closure variables you need to pass them via the param. Some examples

inside an event handler, the longest syntax is:

$('#myelement').click(function() {
   var element = this;
   context.set.filter(function(it) { return it.id_local == this.id; }, 
                      { id: element.id});
});

you can also however omit the this to reference the params as of JayData 1.2 and also use string predicates

$('#myelement').click(function() {
   var element = this;
   context.set.filter("it.id_local == id", { id: element.id});
});

Note that in the string syntax the use of it to denote the lambda argument is mandatory.

In JayData 1.3 we will have an even simplex calling syntax

$('#myelement').click(function() {
   var element = this;
   context.set.filter("it.id_local", "==", element.id);
});
Peter Aron Zentai
  • 11,482
  • 5
  • 41
  • 71
  • What if you got an error like `it.Id == this.Id has no method 'apply'` on a filter like this `var predicate = "it.Id == this.Id"; db.Table.filter(predicate, {Id: 1})` ? – DontVoteMeDown Sep 03 '13 at 21:34
1

In the filter you should pass an object which is the this object, like this:

.filter(function(){},{idABorrar: foo})

foo can be const or any variable which is in scope.

BoltKey
  • 1,994
  • 1
  • 14
  • 26
Gabor Dolla
  • 2,680
  • 4
  • 14
  • 13
0

The .filter() function takes an optional 2nd parameter which is assigned to this inside of the first parameter function.

So you can modify your code like so :

function db_borrarServer(idABorrar){
    serversDB.servidores
        .filter(function(elementoEncontrado) {
            return elementoEncontrado.id_local == this; 
        }, idABorrar)
        .forEach(function(elementoEncontrado){
                console.log('Starting to remove ' + elementoEncontrado.nombre);
                serversDB.servidores.remove(elementoEncontrado);
                serversDB.saveChanges();
        });
}

Let me know how you go - I'm very new to jaydata too and I've also been going a bit crazy trying to get my head into this paradigm. But I came across your question trying to solve the same issue, and this is how I resolved it for me.

kris
  • 11,868
  • 9
  • 88
  • 110