jeudi 8 octobre 2015

Why is 'array' being passed to the filter function?

JSBin Example

In the following code, 'array' is simply an array of integers, 'items' is a list of objects, and 'coprop' is a computed filter of 'items' using 'array' (which may change) to decide which elements of 'items' belong in 'coprop'.

The desired result is just those elements of 'items' that have a 'value' in 'array'.

The actual result is a javascript error when the elements of 'array' are (for some reason) passed to the filtering function in addition to the elements of 'items'. That triggers a 'typeerror: i.get is not function' message.

array: [ 4, 5, 6 ],
items: function() {
    return this.get('store').peekAll('item');
}.property(),
coprop: Ember.computed.filter('items', function(i,idx,ary) {
    console.log('i = ' + i);
    return this.get('array').isAny(i.get('value'));
}).property('items','array')

Please Note This is a simplified example to demonstrate the problem. The filtering function is more complex than shown here, but this does show the issue quite clearly.

Actual Output

"i = <App.Thing:ember409:1>"
"i = <App.Thing:ember410:2>"
"i = <App.Thing:ember411:3>"
"i = <App.Thing:ember412:4>"
"i = 4"
"error"
"TypeError: i.get is not a function

The Questions

Why is 'array' being passed to the filter function as elements to filter?

How do I ensure that 'coprop' will be updated if 'array' changes?




Aucun commentaire:

Enregistrer un commentaire