Skip to content

Fixed unexpected behavior in filtering by substring #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

a-ono
Copy link

@a-ono a-ono commented Dec 2, 2014

#238 is not working as expected.

DS.filter('user', {
  where: {
    name: {
      'in': 'John'
      // expected: user.name.indexOf('John') !== -1
      // actual: 'John'.indexOf(user.name) !== -1
    }
  }
});

@jmdobry
Copy link
Member

jmdobry commented Dec 2, 2014

The current behavior of the in operator follows the pattern used by the rest of the operators in that the logic is constructed with the item property on the left, then the operator, then the term provided in the criteria. Examples:

DS.filter('user', {
  where: {
    age: {
      '<=': 30
    },
    salary: {
      '>': 40000
    },
    name: {
      'in': 'John'
    },
    role: {
      'in': ['admin', 'owner']
    }
  }
});

which translates to:

user.age <= 30 and
user.salary > 40000 and
user.name in 'John' and
user.role in ['admin', 'owner']

which in JavaScript syntax would be:

user.age <= 30 && user.salary > 40000 && 'John'.indexOf(user.name) !== 11 && ['admin', 'owner'].indexOf(user.role) !== -1

The behavior you are looking for would require the addition of a new operator. We could call it the has or contains operator, which would work as follows:

DS.filter('user', {
  where: {
    name: {
      'contains': 'John'
    },
    contractIds: {
      'contains': 12345
    }
  }
});

which translates to:

user.name contains 'John' and
user.contractIds contains 12345

which in JavaScript syntax would be:

user.name.indexOf('John') !== 11 && user.contractIds.indexOf(12345) !== -1

I'll create a ticket for the creation of this new operator.

@jmdobry jmdobry closed this Dec 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants