45791

Search collection and retrieve model backbonejs

Question:

I am trying to search a collection for a model attribute and then grab and return the entire model ?

var myModel = Backbone.Model.extend({ defaults: { a: '', b: '', c: '', d: '', e: '' } });

My collection has around 100 of myModels.

I am trying to search through the collection by a, find it and then return the entire myModel of a so I can access the other attributes ?

Answer1:

If I understand your question correctly, you want to use the where method on Backbone collections, here in the docs:

<a href="http://backbonejs.org/#Collection-where" rel="nofollow">http://backbonejs.org/#Collection-where</a>

So, given an instance of MyCollection called myCollection that has MyModels in it, you can say:

var foundModels = myCollection.where({a:'some value'});

and foundModels will contain an array of the models you seek

BTW, if you are doing a more complex search, use the filter method instead, passing a function as the first argument that returns true on the desired match:

var modelsWhoseAStartsWithA = myCollection.filter(function(anyModel) { var startsWithA = new RegExp(/^[aA]/); return startsWithA.test(anyModel.get('a')); });

Recommend

  • How to get a data-id attribute with jQuery
  • Rendering in backbone.js with underscore templates
  • jquery mobile datebox intercepting the date select click
  • Getting all python-rom objects into a list
  • Default error page
  • How to add extra data to a Django model for display in template?
  • Search collection and retrieve model backbonejs
  • How to re-render views using Angular JS
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • how to get data attributes of dynamically generated element
  • ListItem.Attributes.Add not working
  • Redux Form - Not able to type anything in input
  • How to change the font size of a single index for UISegmentedControl?
  • How can I sort a a table with VBA with given text condition?
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • Ajax Loaded meta Tags
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Using jQuery closest() method with class selector
  • Xamarin Forms - UWP Fonts
  • Array.prototype.includes - not transformed with babel
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Arrays break string types in Julia
  • How to get icons for entities from eclipse?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Benchmarking RAM performance - UWP and C#
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Turn off referential integrity in Derby? is it possible?
  • python draw pie shapes with colour filled
  • JaxB to read class hierarchy
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?