337 Commits

Author SHA1 Message Date
Julian Gonggrijp
19ce298f78 Address linter errors and warnings (#4251 #4246) 2022-02-18 17:15:59 +01:00
Paul Falgout
7712b8ec6c Add model changeId event
Fixes #3882
Fixes #4159

Backbone does a bit of extra work to determine when to update `_byId` on every model change event because `change:id` will not work if `idAttribute` has changed.  This causes issues as the `change` event happens after every `change:` event which means during a change the `_byId` hasn't updated.  Rather than adding complexity to collection the solution is to have the model notify with the id changes.

If adding a public event isn't desired, for an internal solution the model is aware of it's collection and could modify model.collection._byId directly within the set.

Either of these solutions seem preferrable to handling `change:[idAttribute]`

Replaces the need for:
https://github.com/jashkenas/backbone/pull/4227/files#diff-c773bb9be277f0f3f2baa308b6e0f3a486790fe99fea81ddd0ba409846250571R1205
2022-01-12 23:26:10 +09:00
Alexey
2ea3a221ad Remove try..catch 2022-01-09 20:17:55 +03:00
Alexey
e54031f227 Return function 2022-01-09 20:14:20 +03:00
Alexey
4515b8c20a Fix synatx error 2022-01-09 20:13:45 +03:00
Alexey
f6f5deeae9 Add test 2022-01-08 12:42:00 +03:00
jashkenas
0b1c2e3fce Resolve merge conflicts 2019-12-13 11:41:21 -08:00
Graeme Yeates
e524804592 Implement Model.mixin and Collection.mixin (#4024)
Combine implementation of collection and model underscore methods
2017-04-18 15:29:55 -07:00
ksladkov
7da4ed73e7 #4103 check if obj is Model in Collection.get (#4104) 2016-12-01 16:01:59 -05:00
Lee Byron
a2fc65b979 Implements the JavaScript Iterable protocol.
This introduces new methods related to Iterators on Backbone.Collection to mirror those found on Array: `values`, `keys`, `entries`, and `@@iterator`. Each of these methods will return a JavaScript Iterator, which has a `next` method, yielding the models or ids of models contained in the Collection.

The CollectionIterator is careful to use the `at()` and `modelId()` methods on the host collection rather than direct access to the `models` property, which should ensure it is resilient to creative subclassing of Backbone.Collection and future feature addition.

The [`@@iterator`](http://www.ecma-international.org/ecma-262/6.0/#sec-well-known-symbols) method is defined using `Symbol.iterator` if it exists in the JavaScript runtime (modern browsers/node.js) and falls back to the string `"@@iterator"` which was popularized by older versions of Firefox and has become the standard fallback behavior for other third-party libraries. This ensures that Backbone can still be used across all browsers, even with use of these new methods.

Supporting Iterable allows better integration between Backbone and the most recent additions to the JavaScript language, including `for of` loops and data-collection constructor functions, as well as better integration with other third-party libraries that accept Iterables instead of only Arrays.

Fixes #3954
2016-07-21 13:33:30 -07:00
Adam Krebs
02891a2924 cleanup some automatic test failures, fix lint issues 2016-06-20 16:17:15 -04:00
Adam Krebs
55bab76349 Upgrade Qunit to 2.0, auto-convert tests using apsdehal/qunit-migrate 2016-06-20 16:04:30 -04:00
Ben McCormick
f6d8c3e51d Add a preinitialize function to allow modifying Backbone objects before
instantiation logic is run.

This allows greater flexibility for several use cases including
- #3620
- #3685
- Easing use of ES6 classes with Backbone
2016-04-24 22:26:51 -04:00
Adam Krebs
a640b3bdd4 use _.indexOf instead of Array.prototype.indexOf in test. Fixes failing IE8 case 2016-03-29 14:50:06 -04:00
Adam Miller
b3a2813290 Only pass idAttribute to collection.modelId() 2016-02-24 17:36:51 -08:00
Adam Miller
c71336641e Update to modelId()
- modelId now takes the model in question as the second argument
 - modelId will look at the model in question's idAttribute before falling back to Collection.prototype.model.modelId and then 'id'
 - Makes basic polymorphic collection work out of the box while retaining backwards compatability
2016-02-09 13:02:06 -08:00
Ash Lux
32d89dc47e When listening to numerous events and stopListening is called, then _.isEmpty is painfully slowly and assigning _listenTo to undefined isn't necessary. 2016-02-08 15:16:10 -05:00
Adam Krebs
8d8b854abf rename at => val in test to be clearer 2016-02-04 17:16:06 -05:00
Craig Martin
8932ae4bc3 optimize collection get and account for edge case 2016-02-04 15:30:26 -05:00
Craig Martin
47fb04a373 fix failing linting tests 2016-02-04 15:16:41 -05:00
Adam Krebs
b0161947eb Fix #3951: options.index should never exceed collection.length 2016-02-04 15:01:59 -05:00
Graeme Yeates
670ff30aa4 Improve collection 'update' event consistency 2016-02-01 11:33:39 -05:00
Linus-A. M. Gubenis
90cb566250 Pass added and removed models to collection update events 2016-02-01 11:18:09 -05:00
Craig Martin
1aa7f7685a add tests solidifying behavior for modelId with polymorphic models 2016-01-20 09:23:50 -05:00
Adam Krebs
82d8ec840c Merge pull request #3929 from craigmichaelmartin/avoid-redefinitions
eliminate shadowed variable declarations
2016-01-19 13:56:36 -05:00
Craig Martin
e0a5cc75ba eliminate shadowed variable declarations
Also, add eslint rule no-shadow to enforce
2016-01-19 12:34:08 -05:00
Craig Martin
75666b08c9 add has method to collection 2016-01-17 12:19:36 -05:00
Jordan Eldredge
be1f2a87c5 Add ESLint rule: eqeqeq
Underscore uses this triple-equals syntax for its module loader.
2015-12-28 21:37:17 -08:00
Jordan Eldredge
37465828d6 Add ESLint warning for quoted keys to tests
I've opted to leave the quotes and associated warnings in the case where the
keys include protected words or integers.
2015-12-18 13:27:48 -08:00
Jordan Eldredge
9eb9124ec9 Enforce camelCase ESLint rule in tests 2015-12-18 13:27:48 -08:00
Jordan Eldredge
3dbdfab21b Enable ESLint semicolons for tests 2015-12-18 13:27:48 -08:00
Jordan Eldredge
d91405d54f Enable ESLint rule: 'quotes' for tests
These changes were done automatically by ESLint's `--fix` tag. Thanks ESLint!
2015-12-18 13:27:48 -08:00
Jordan Eldredge
7dbdd0ac6b Add ESLint rule: no-undef 2015-12-18 13:27:48 -08:00
Jordan Eldredge
2f5cd8bee9 Enable ESLint rule: new-cap
Constructor functions must start with a capital letter.
2015-12-18 08:35:28 -08:00
Jordan Eldredge
2ae117a7cb Add ESLint rule: key-spacing 2015-12-16 10:00:19 -08:00
Jordan Eldredge
b0a6274290 Non-controversial ESLint whitespace rules
In an attempt to bring our ESLint rules closer to those in Underscore, I've
started by adding some whitespace rules which don't involve any significantly
opinionated changes.
2015-12-16 07:43:26 -08:00
Ilia Ablamonov
19b88d2c2d Fixed Collection#set regression when parse returns falsy value
Fixes #3871
2015-12-01 10:00:36 +04:00
Justin Ridgewell
569f07e6a6 Merge pull request #3820 from jridgewell/reduce-deopt
Deopt Underscore methods that change on arguments
2015-10-28 09:36:49 -05:00
Graeme Yeates
d1880dda6c Merge pull request #3834 from jridgewell/model-change-id-regression
Fix _onModelEvent regression
2015-10-26 15:18:41 -04:00
Justin Ridgewell
67667c5b8f Fix _onModelEvent regression
Fixes #3662.
2015-10-26 15:13:17 -04:00
Justin Ridgewell
7bbbcc8c51 Always return removed array from #_removeModels
Fixes #3835.
2015-10-26 11:19:26 -04:00
Justin Ridgewell
2e60498b3d Merge pull request #3821 from captbaritone/indent-fix
Fix indention in tests
2015-10-07 11:04:18 -05:00
Jordan Eldredge
2984d95a85 Fix indention in tests 2015-10-07 08:57:41 -07:00
Jordan Eldredge
4b6ad311bd Document using [] for empty Collections with options
The documented example shows passing `[]` to instantiate an empty collection
while still supplying an options object. This change makes the preceding
documentation consistent with that example.

I also noticed that we don't have any tests covering the previously recommended
approach of passing `null` (or any falsey value) for models. I've added a test
to ensure we continue to support that usage.
2015-10-07 08:49:52 -07:00
just-boris
345dd7b128 Allow to make reduce without initial value 2015-10-07 10:59:00 -04:00
Kevin Partington
9c681906a4 Tests: Migrated unit tests to QUnit 2.0 syntax. (fixes #3813) 2015-10-06 23:35:30 -05:00
Jacob Buck
6cff9419fb Add findIndex and findLastIndex underscore methods to Collection. 2015-09-30 10:20:33 +13:00
Jeremy Ashkenas
979adf5200 Merge pull request #3803 from jridgewell/removeModels-regression
Fix _removeModels regression
2015-09-25 10:48:48 -04:00
Justin Ridgewell
4b64eebcba Fix _removeModels regression
Fixes #3693.

This leaves open the question of whether events triggered on the model
during a ‘remove’ listener should also trigger on the model. Just
something to revisit for V2.

```js
col.on('other', (model) => {
// Should this be triggered?
});

col.on('remove', (model) => {
// If the model is really "removed" (we can't `#get` it anymore)
// by the time this listener is called, then I'd argue that this
// shouldn't trigger the 'other' event on the collection...
model.trigger('other');
});
```
2015-09-23 11:24:16 -04:00
Andrew Humphreys
91d0824190 fix error when adding model to collection at an out of bounds index 2015-08-21 17:15:07 +01:00