简体   繁体   中英

Ember Data: Filtering on hasMany by id without fetching object

I have a model with a hasMany collection, and I would like to determine whether or not an object exists in that collection, given the ID of an object.

var searchFor = "someid";
var filtered = parent.get('children').filter(function (item) { return item.get('child.id') == searchFor; });
var exists = filter.get('length') > 0;

However, this makes a remote request to the child endpoint with each iteration of the filter, which is unnecessary since it knows the IDs already. Is there any way to work around this?

I've been dealing with the same problem. The best thing I've come up with is:

var searchFor = "someid";
var filtered = parent.get('children').filter(function (item) {
    return item.get('data').child.id == searchFor;
});
var exists = filtered.get('length') > 0;

This works (ie doesn't make a request), but feels wrong. This works for me with Ember 1.4.0 and Ember Data 1.0.0-beta.3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM