8000 fix(): collection fires remove event (#530) · correct-js/js-data@e2f41d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit e2f41d0

Browse files
authored
fix(): collection fires remove event (js-data#530)
1 parent d3c6484 commit e2f41d0

File tree

8 files changed

+86
-35
lines changed

8 files changed

+86
-35
lines changed

dist/js-data.es2015.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js-data.es2015.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js-data.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js-data.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js-data.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js-data.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Collection.js

Lines changed: 48 additions & 18 deletions
763
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,19 @@ export default Component.extend({
239239
records = [records]
240240
singular = true
241241
} else {
242-
throw utils.err(`${DOMAIN}#add`, 'records')(400, 'object or array', records)
242+
throw utils.err(`${DOMAIN}#add`, 'records')(
243+
400,
244+
'object or array',
245+
records
246+
)
243247
}
244248
}
245249

246250
// Map the provided records to existing records.
247251
// New records will be inserted. If any records map to existing records,
248252
// they will be merged into the existing records according to the onConflict
249253
// option.
250-
records = records.map((record) => {
254+
records = records.map(record => {
251255
let id = this.recordId(record)
252256
// Grab existing record if there is one
253257
const existing = id === undefined ? id : this.get(id)
@@ -261,8 +265,17 @@ export default Component.extend({
261265
// Here, the currently visited record corresponds to a record already
262266
// in the collection, so we need to merge them
263267
const onConflict = opts.onConflict || this.onConflict
264-
if (onConflict !== 'merge' && onConflict !== 'replace' && onConflict !== 'skip') {
265-
throw utils.err(`${DOMAIN}#add`, 'opts.onConflict')(400, 'one of (merge, replace, skip)', onConflict, true)
268+
if (
269+
onConflict !== 'merge' &&
270+
onConflict !== 'replace' &&
271+
onConflict !== 'skip'
272+
) {
273+
throw utils.err(`${DOMAIN}#add`, 'opts.onConflict')(
274+
400,
275+
'one of (merge, replace, skip)',
276+
onConflict,
277+
true
278+
)
266279
}
267280
const existingNoValidate = existing._get(noValidatePath)
268281
if (opts.noValidate) {
@@ -411,7 +424,9 @@ export default Component.extend({
411424
* @returns {Object[]|Record[]} The result.
412425
*/
413426
between (leftKeys, rightKeys, opts) {
414-
return this.query().between(leftKeys, rightKeys, opts).run()
427+
return this.query()
428+
.between(leftKeys, rightKeys, opts)
429+
.run()
415430
},
416431

417432
/**
@@ -437,8 +452,8 @@ export default Component.extend({
437452
fieldList = [name]
438453
}
439454
opts || (opts = {})
440-
opts.hashCode || (opts.hashCode = (obj) => this.recordId(obj))
441-
const index = this.indexes[name] = new Index(fieldList, opts)
455+
opts.hashCode || (opts.hashCode = obj => this.recordId(obj))
456+
const index = (this.indexes[name] = new Index(fieldList, opts))
442457
this.index.visitAll(index.insertRecord, index)
443458
},
444459

@@ -483,7 +498,9 @@ export default Component.extend({
483498
* @since 3.0.0
484499
*/
485500
filter (query, thisArg) {
486-
return this.query().filter(query, thisArg).run()
501+
return this.query()
502+
.filter(query, thisArg)
503+
.run()
487504
},
488505

489506
/**
@@ -513,7 +530,12 @@ export default Component.extend({
513530
* @returns {(Object|Record)} The record with the given id.
514531
*/
515532
get (id) {
516-
const instances = id === undefined ? [] : this.query().get(id).run()
533+
const instances =
534+
id === undefined
535+
? []
536+
: this.query()
537+
.get(id)
538+
.run()
517539
return instances.length ? instances[0] : undefined
518540
},
519541

@@ -541,7 +563,9 @@ export default Component.extend({
541563
* @returns {Array} The result.
542564
*/
543565
getAll (...args) {
544-
return this.query().getAll(...args).run()
566+
return this.query()
567+
.getAll(...args)
568+
.run()
545569
},
546570

547571
/**
@@ -574,7 +598,9 @@ export default Component.extend({
574598
* @returns {Array} The result.
575599
*/
576600
limit (num) {
577-
return this.query().limit(num).run()
601+
return this.query()
602+
.limit(num)
603+
.run()
578604
},
579605

580606
/**
@@ -710,9 +736,9 @@ export default Component.extend({
710736
})
711737
if (utils.isFunction(record.off)) {
712738
record.off('all', this._onRecordEvent, this)
713-
if (!opts.silent) {
714-
this.emit('remove', record)
715-
}
739+
}
740+
if (!opts.silent) {
741+
this.emit('remove', record)
716742
}
717743
}
718744
}
@@ -737,14 +763,16 @@ export default Component.extend({
737
// Default values for arguments
738764
opts || (opts = {})
739765
this.beforeRemoveAll(queryOrRecords, opts)
740-
let records = utils.isArray(queryOrRecords) ? queryOrRecords.slice() : this.filter(queryOrRecords)
766+
let records = utils.isArray(queryOrRecords)
767+
? queryOrRecords.slice()
768+
: this.filter(queryOrRecords)
741769

742770
// Remove each selected record from the collection
743771
const optsCopy = utils.plainCopy(opts)
744772
optsCopy.silent = true
745773
records = records
746-
.map((record) => this.remove(record, optsCopy))
747-
.filter((record) => record)
774+
.map(record => this.remove(record, optsCopy))
775+
.filter(record => record)
748776
if (!opts.silent) {
749777
this.emit('remove', records)
750778
}
@@ -765,7 +793,9 @@ export default Component.extend({
765793
* @returns {Array} The result.
766794
*/
767795
skip (num) {
768-
return this.query().skip(num).run()
796+
return this.query()
797+
.skip(num)
798+
.run()
769799
},
770800

771801
/**

test/integration/collection.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ describe('Collection integration tests', function () {
4747
}, 30)
4848
})
4949

50+
it('should emit remove events', function (done) {
51+
const mapper = new JSData.Mapper({ name: 'user' })
52+
const data = mapper.createRecord({ id: 2, age: 19 })
53+
const collection = new JSData.Collection([], {
54+
mapper
55+
})
56+
const listener = sinon.stub()
57+
const listener2 = sinon.stub()
58+
collection.add(data)
59+
collection.on('remove', listener)
60+
collection.on('all', listener2)
61+
const records = collection.remove(data)
62+
setTimeout(() => {
63+
assert(listener.calledOnce, 'listener should have been called once')
64+
assert.deepEqual(listener.firstCall.args, [records], 'should have been called with the correct args')
65+
assert(listener2.calledOnce, 'listener2 should have been called once')
66+
assert.deepEqual(listener2.firstCall.args, ['remove', records], 'should have been called with the correct args')
67+
done()
68+
}, 30)
69+
})
70+
5071
it('should bubble up record events', function (done) {
5172
const mapper = new JSData.Mapper({ name: 'user' })
5273
const data = [

0 commit comments

Comments
 (0)
0