jashkenas / jashkenas/backbone

collection.prototype.chain() doesn't proxy requests to model.attributes

Open
#4,016 4 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
JavaScript
Stars
28.1k
Forks
5.3k
PR merge metrics
No merged PRs in 30d

Description

As far as I can tell, it looks like most of the underscore methods attached to collections are proxied so that when you call them they act on the models' `.attributes` object. This is great because it lets us do things like:

``` javascript
collection.where({ foo: 'bar' }); // Yay! Looks at model.attributes.foo
```

Unfortunately, it seems like all of this breaks when you call `.chain()`

``` javascript
collection.chain().where({ foo: 'bar' }).value(); // Boo :( Looks at model.foo
```

Obviously this example isn't very useful, but if you want to do more than one operation on the collection you can't use any of the shortcuts for things like `where`, `pluck`, etc and need to use iterator functions which is so much less clean.

Contributor guide

Open the contributing guide

Research direction

Start by tracing collection.prototype.chain() and the collection proxies for methods such as where and pluck; compare how chained calls handle model.attributes with direct collection calls. Reproduce the where example and verify that chained where(...).value() evaluates against model.attributes while preserving the existing collection shortcuts.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.