JavaScript Retrieve list of a specific attribute from a Backbone collection

I'm looking for a way to, from a backbone collection, retrieve some kind of array of one specific attribute.

var SomeModel = Backbone.Model.extend({
    defaults: function() {
        return {
            attr1: "something",
            attr2: "something",
            attr3: "darkside"
        };
    }
});

var SomeCollection = Backbone.Collection.extend({
    model: SomeModel,
    url: '/data.json',
});

Now assume I have something like above and I only want a collection/array/similar with only the attribute 'attr3'. Is there any nice way to get this (not by manually iterating through the SomeCollection and building a new Collection)? My understanding from underscore function "filter" is that it returns the entire SomeModel based on a certain premise, i.e. this is not what I want. Ideas?

Thanks

Answer:1

If you only want one attribute, you can use _.pluck

SomeCollection.pluck('attr3');

For multiple attributes, you can combine _.map on collections and _.pick on models

SomeCollection.map(function (model) {
    return model.pick('attr2', 'attr3');
});

And a demo http://jsfiddle.net/nikoshr/qpyXc/1/

Or, for a more concise version using _.invoke

SomeCollection.invoke('pick', 'attr2', 'attr3');

http://jsfiddle.net/nikoshr/qpyXc/3/

Answer:2

I think you want Underscore's pluck:

A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.

var attrs = _.pluck(someCollection.models, 'attr3');
Answer:3

I think there is some misunderstanding here. In the code you posted, you setup default attributes for newly created models. Any model created via:

var model = new SomeModel({});

or:

var collection = new SomeCollection();
// Add to collection
collection.add({});
// Add to collection AND sync with server
collection.create({});

will have those attributes set to given values, given that you don't define them differently in the hash passed to above functions.

Instead, I think you're asking for this:

// Create some models
var model_a = new SomeModel({attr1: "Alpha"});
var model_b = new SomeModel({attr1: "Beta"});
var model_c = new SomeModel({attr1: "Gamma"});

// Push them in the collection
var collection = new SomeCollection([model_a, model_b, model_c]);

THEN you can get an array of strings containing values for requested models attribute with:

collection.pluck(attributeName);

Which is itself a function inherited from Underscore.js, on which Backbone relies.

I specify this to make clear that _.pluck(array, attributeName) is available also outside Backbone context and works on arrays, not collections. Internally, collection.pluck calls _.pluck(collection.models, fieldName).

Answer:4

To see the result, open console in Google Chrome, do the following: obj = new Boolean(false) "obj && true: " + (obj && true) "obj || true: " + (obj || false) and: (obj && ...

To see the result, open console in Google Chrome, do the following: obj = new Boolean(false) "obj && true: " + (obj && true) "obj || true: " + (obj || false) and: (obj && ...

  1. javascript snake
  2. javascript foreach
  3. javascript map
  4. javascript for loop
  5. javascript array
  6. javascript substring
  7. javascript switch
  8. javascript reduce

I have the following HTML: <ul> <li></li> <li></li> <li></li> </ul> and the following jQuery code: $('> ul li', body).each(function() { $('...

I have the following HTML: <ul> <li></li> <li></li> <li></li> </ul> and the following jQuery code: $('> ul li', body).each(function() { $('...

  1. jquery swap tag
  2. jquery switch tag

I have a datepicker, and I need to be able to enter a date manually in multiple different formats. For example, for May 7, 2015, I need to be able to enter: 05/07/2015 05072015 050715 5715 I ...

I have a datepicker, and I need to be able to enter a date manually in multiple different formats. For example, for May 7, 2015, I need to be able to enter: 05/07/2015 05072015 050715 5715 I ...

I want to send some JS Script from controller to GSP view . I do the following attempt. Controller(Purchase.groovy) : def myaction={ flash.script= 'jQuery("div#header").show(1000);' ...

I want to send some JS Script from controller to GSP view . I do the following attempt. Controller(Purchase.groovy) : def myaction={ flash.script= 'jQuery("div#header").show(1000);' ...

  1. send email javascript code
  2. send mail javascript code
  3. send email using javascript code
  4. send javascript alert from code behind c#
  5. send email through javascript code
  6. javascript send html code
  7. send mail using javascript code
  8. javascript send key code
  9. javascript send error code
  10. node.js send javascript code
  11. send verification code javascript
  12. send email html code javascript
  13. javascript send value to code behind