Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection seems chainable in the Chainable API example #669

Open
ravenjam opened this issue Jul 17, 2015 · 0 comments
Open

Collection seems chainable in the Chainable API example #669

ravenjam opened this issue Jul 17, 2015 · 0 comments

Comments

@ravenjam
Copy link

In Chainable API example I saw this:

var collection = new Backbone.Collection([
  { name: 'Tim', age: 5 },
  { name: 'Ida', age: 26 },
  { name: 'Rob', age: 55 }
]);

var filteredNames = collection.chain() // start chain, returns wrapper around collection's models
  .filter(function(item) { return item.get('age') > 10; }) // returns wrapped array excluding Tim
  .map(function(item) { return item.get('name'); }) // returns wrapped array containing remaining names
  .value(); // terminates the chain and returns the resulting array

console.log(filteredNames); // logs: ['Ida', 'Rob']

Therefore I tested to just use the collection without the chain() method. It seems that it worked well. I ran this in the console:

var collection = new Backbone.Collection([
  { name: 'Tim', age: 5 },
  { name: 'Ida', age: 26 },
  { name: 'Rob', age: 55 }
]);

collection
  .filter(function(item) { return item.get('age') > 10; })
  .map(function(item) { return item.get('name'); }) ;

and it returns ['Ida', 'Rob'].

I'm wondering why? It seems that the chain() method is not necessary? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant