1

I'm trying to create a generic filter Value Converter to use in a few cases. I would like to be able filter the items in the array by properties on sub objects like this:

<li repeat.for="row of router.navigation | filter:'settings.where':'top'" >

I know I'm going to have to parse the settings.where expression to get to the value. So far I have cobbled together the following:

import {inject, Parser} from 'aurelia-framework'

@inject(Parser)
export class FilterValueConverter {
  constructor(parser) {
    this.parser = parser;
  }

  toView(array, property, exp) {    
    let expression = this.parser.parse(property);
    return array.filter((item) => expression.evaluate(...?) === exp);
  }
}

The parse appears to give me an expression but was the Parser designed to be used outside the core framework? evaluate requires a scope and I don't have one of those... I could walk the expression tree and get the result myself but does something like this already exist?

Tim
  • 7,746
  • 3
  • 49
  • 83

2 Answers2

1

This is a snipped from an aurelia blog post about value converters:

enter image description here

Maybe this helps you, with that in mind you should be able to use your example just without quotes:

<li repeat.for="row of router.navigation | filter:settings.where:'top'" >

kabaehr
  • 1,040
  • 11
  • 18
  • That example shows how to pass in a single sub-property value to the value converter. I'm trying to pass in a path than can be evaluated for each item in the array and used to filter each of them. – Tim Apr 07 '16 at 21:14
  • Could you explain a little more what you want to achieve and how this pathes you want to be evaluated can look like? – kabaehr Apr 08 '16 at 06:21
  • I have a filter value converter like the one in this answer: http://stackoverflow.com/a/29163491/106623. It uses `item[property]` to get a value on each item to compare to. This will not work for sub properties `item['settings.where']` as it not valid JavaScript to access `item.settings.where`. The Parser in aurelia can already parse `settings.where` into an expression but I cant evaluate it without a scope. – Tim Apr 08 '16 at 12:42
  • @Tim what if you split your string by `.` and then use something like `item[property[0]][property[1]]`?. Does that work for you? – Fabio Apr 08 '16 at 23:59
  • I could do it manually but it would still be restricted. What if I wanted to use something like `settings[0].where` as the path? The great thing with the existing parser is it can already deal with this... – Tim Apr 09 '16 at 18:22
0

I finally had a chance to look at the framework source I was very nearly there. The code below appears to work. Scope simply requires a bindingContext:

import {inject, Parser} from 'aurelia-framework'

@inject(Parser)
export class FilterValueConverter {
  constructor(parser) {
    this.parser = parser;
  }

  toView(array, property, exp) {    
    let expression = this.parser.parse(property);
    return array.filter((item) => expression.evaluate({bindingContext:item}) === exp);
  }
}
Tim
  • 7,746
  • 3
  • 49
  • 83