简体   繁体   English

使用子字符串的骨架.js过滤器集合

[英]backbone.js filter collection using substring

The following problem is driving me crazy. 以下问题使我发疯。

_.each(collection, function( account, key ){

    var totalPhy = that.physicianCollection.where({ 'Hospital_Id__c' : account.Id }).length;
    account.physicians = { 'total' : totalPhy };
});

It is working when Hospital_Id__c same as account.Id. Hospital_Id__c与account.Id相同时,它正在工作。 But my account Id is a sub string of the hospital_Id__c . 但是我的帐户ID是hospital_Id__c的子字符串。 How do I search and get the count? 我如何搜索并获得计数? I tried index of and search methods. 我尝试了索引和搜索方法。 Pls suggest. 请建议。 Thanks in advance. 提前致谢。

_.where is a simple use case of _.filter to match exact properties. _.where是一个简单的用例的_.filter精确匹配属性。 In your case you will need to actually use _.filter and write the logic yourself. 在您的情况下,您将需要实际使用_.filter并自己编写逻辑。 I'm not sure what the account id / hospital id look like, but the code will probably look something like : 我不确定帐户ID /医院ID是什么样的,但是代码可能看起来像:

var totalPhy = that.physicianCollection.filter(function(phys, index, collection){
    //phys is your model
    return phys.get('Hospital_Id__c').indexOf(account.Id) != -1; 
    //(or however the ids are set, your logic here)
}).length;
account.physicians = { 'total' : totalPhy };

http://underscorejs.org/#filter http://underscorejs.org/#filter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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