Skip to content
This repository

Allow Model#get to accept multiple arguments #2232

Closed
wants to merge 2 commits into from

2 participants

Adam Krebs Tim Griesser
Adam Krebs
Collaborator

Finally growing frustrated with seeing code blocks like this

var a = book.get('a');
var b = book.get('b');
var c = book.get('c');

doSomething(a);
doSomething(b);
doSomething(c);

in my code, I decided to write up a quick pull to allow this beautifulness instead:

_.each(book.get('a', 'b', 'c'), doSomething);

or just

var letters = book.get('a', 'b', 'c');
console.log(letters); // ['foo', 'bar', 'baz']

This will give Model#get closer feature parity with Model#set, which can take multiple attribute/value pairs as arguments to assign.

Tim Griesser
Collaborator

I think this would be better addressed with pick, if some of the proposed underscore methods in your request on #2082 are merged.

Adam Krebs
Collaborator
Adam Krebs
Collaborator

I don't really have a solution to the get/has semantics. I'd still prefer get to be overloaded instead of using _.values(model.pick('name', 'title')), but as get handles a lot in Backbone internals, I guess I'll have to deal with a little more typing for now.

Adam Krebs akre54 closed this February 02, 2013
Adam Krebs akre54 deleted the branch February 02, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
7  backbone.js
@@ -277,8 +277,11 @@
277 277
     },
278 278
 
279 279
     // Get the value of an attribute.
280  
-    get: function(attr) {
281  
-      return this.attributes[attr];
  280
+    get: function() {
  281
+      var attrs = _.map(slice.call(arguments), function(attr) {
  282
+        return this.attributes[attr];
  283
+      }, this);
  284
+      return attrs.length === 1 ? attrs[0] : attrs;
282 285
     },
283 286
 
284 287
     // Get the HTML-escaped value of an attribute.
3  test/model.js
@@ -143,9 +143,10 @@ $(document).ready(function() {
143 143
     ok(!new Backbone.Model({ 'id': -5 }).isNew(), "is false for a negative integer");
144 144
   });
145 145
 
146  
-  test("get", 2, function() {
  146
+  test("get", 3, function() {
147 147
     equal(doc.get('title'), 'The Tempest');
148 148
     equal(doc.get('author'), 'Bill Shakespeare');
  149
+    deepEqual(doc.get('title', 'author'), ['The Tempest', 'Bill Shakespeare']);
149 150
   });
150 151
 
151 152
   test("escape", 5, function() {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.