javascript - breaking out of an underscore each - Stack Overflow

I am trying to finda model within a collection with an attribute equal to html select option value.&l

I am trying to find a model within a collection with an attribute equal to html select option value.

<div id="hospital-details">
    <select name="hospitalnames">
       <option><%- model.get('name') %></option>
    </select>
</div>

whenever hospital name is changed, jquery change callback is triggered to find locationModel with selected option value as attribute value as shown below,

$('select[name="hospitalnames"]').change(function() {
   var name =  $(this).val();
   locationListCollection.each(function(locationModel) {
     if ($.trim(locationModel.get('name')) == $.trim(name)) {
        that.locationModel = locationModel;
        return false; // control is returned to underscore.min.js
     }
   });
});
console.log(that.locationModel); // this is not being displayed at all

After the locationModel with an attribute is found, I am unable to e out the loop. Any help ? At this moment I have looked into this but without success.

I am trying to find a model within a collection with an attribute equal to html select option value.

<div id="hospital-details">
    <select name="hospitalnames">
       <option><%- model.get('name') %></option>
    </select>
</div>

whenever hospital name is changed, jquery change callback is triggered to find locationModel with selected option value as attribute value as shown below,

$('select[name="hospitalnames"]').change(function() {
   var name =  $(this).val();
   locationListCollection.each(function(locationModel) {
     if ($.trim(locationModel.get('name')) == $.trim(name)) {
        that.locationModel = locationModel;
        return false; // control is returned to underscore.min.js
     }
   });
});
console.log(that.locationModel); // this is not being displayed at all

After the locationModel with an attribute is found, I am unable to e out the loop. Any help ? At this moment I have looked into this but without success.

Share Improve this question edited May 23, 2017 at 11:47 CommunityBot 11 silver badge asked Oct 9, 2013 at 4:47 geekgugigeekgugi 3891 gold badge6 silver badges23 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 9

You're using the wrong method if you're searching for the first match. Collections have lots of Underscore methods mixed in, in particular they have find mixed in:

find _.find(list, iterator, [context])

Looks through each value in the list, returning the first one that passes a truth test (iterator), or undefined if no value passes the test.

Something like this:

var name = $.trim($(this).val());
that.locationModel = locationListCollection.find(function(locationModel) {
  return $.trim(locationModel.get('name')) == name;
});

and if the names in your model are pre-trimmed and nice and clean, then you could use findWhere:

findWhere collection.findWhere(attributes)

Just like where, but directly returns only the first model in the collection that matches the passed attributes.

like this:

var name = $.trim($(this).val());
that.locationModel = locationListCollection.findWhere({ name: name });

BTW, this:

console.log(locationModel);

won't give you anything because locationModel and that.locationModel are different things.

You can always go oldschool.

$('select[name="hospitalnames"]').change(function() {
   var name =  $(this).val();
   for (var i = 0; i < locationListCollection.length; ++i) {
     var locationModel = locationListCollection.models[i];
     if ($.trim(locationModel.get('name')) == $.trim(name)) {
        that.locationModel = locationModel;
        break;
     }
   }
});

Try this,

var name =  $(this).val();
var flag=true;
locationListCollection.each(function(locationModel) {
  if (flag && $.trim(locationModel.get('name')) == $.trim(name)) {
     that.locationModel = locationModel;
     flag=false;
      //return false;// to break the $.each loop
  }
});

The short is no.

If you take a look at underscore's source you'll see that they use a breaker object to quickly stop a .each() but that is only available internally.

I would not remend this but you could always modify the source to expose this breaker object (see baseline setup in the annotated source http://underscorejs/docs/underscore.html). Then you would just return this object instead of returning false. But you would probably need to remove the native forEach call to keep the behaviour consistent. So it's not worth it!

_.each(function(arr) {
    if(condition) {
       return _.breaker; // Assuming you changed the source.
    }
});

Since you are searching for a single item instead of .each() use:

 var locationModel = _.find(arr, function(item) {
    return $.trim(locationModel.get('name')) == $.trim(name);
 ));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743643103a4483298.html

相关推荐

  • javascript - breaking out of an underscore each - Stack Overflow

    I am trying to finda model within a collection with an attribute equal to html select option value.&l

    4天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信