From 879210c99f2d873f5062a6c03b6a345c43d578c8 Mon Sep 17 00:00:00 2001 From: Eli Skeggs Date: Tue, 8 Sep 2020 12:24:54 -0700 Subject: [PATCH] refactor: prefer this over bound this --- src/Collection.js | 18 +++++++----------- src/Model.js | 37 +++++++++++++++++-------------------- src/sync.js | 2 +- 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/Collection.js b/src/Collection.js index 3b2b53fb2..4e5f6e34c 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -44,9 +44,7 @@ export const Collection = /*#__PURE__*/ (() => { // The JSON representation of a Collection is an array of the // models' attributes. toJSON(options) { - return this.map(function (model) { - return model.toJSON(options); - }); + return this.map((model) => model.toJSON(options)); } // Proxy `sync` by default. @@ -300,12 +298,11 @@ export const Collection = /*#__PURE__*/ (() => { fetch(options) { options = { parse: true, ...options }; const success = options.success; - const collection = this; - options.success = function (resp) { + options.success = (resp) => { const method = options.reset ? 'reset' : 'set'; - collection[method](resp, options); - if (success) success.call(options.context, collection, resp, options); - collection.trigger('sync', collection, resp, options); + this[method](resp, options); + if (success) success.call(options.context, this, resp, options); + this.trigger('sync', this, resp, options); }; wrapError(this, options); return this.sync('read', this, options); @@ -320,10 +317,9 @@ export const Collection = /*#__PURE__*/ (() => { model = this._prepareModel(model, options); if (!model) return false; if (!wait) this.add(model, options); - const collection = this; const success = options.success; - options.success = function (m, resp, callbackOpts) { - if (wait) collection.add(m, callbackOpts); + options.success = (m, resp, callbackOpts) => { + if (wait) this.add(m, callbackOpts); if (success) success.call(callbackOpts.context, m, resp, callbackOpts); }; model.save(null, options); diff --git a/src/Model.js b/src/Model.js index 195d94c44..93ad3698d 100644 --- a/src/Model.js +++ b/src/Model.js @@ -198,13 +198,12 @@ export const Model = /*#__PURE__*/ (() => { // local attributes. Any changed attributes will trigger a "change" event. fetch(options) { options = { parse: true, ...options }; - const model = this; const success = options.success; - options.success = function (resp) { - const serverAttrs = options.parse ? model.parse(resp, options) : resp; - if (!model.set(serverAttrs, options)) return false; - if (success) success.call(options.context, model, resp, options); - model.trigger('sync', model, resp, options); + options.success = (resp) => { + const serverAttrs = options.parse ? this.parse(resp, options) : resp; + if (!this.set(serverAttrs, options)) return false; + if (success) success.call(options.context, this, resp, options); + this.trigger('sync', this, resp, options); }; wrapError(this, options); return this.sync('read', this, options); @@ -237,17 +236,16 @@ export const Model = /*#__PURE__*/ (() => { // After a successful server-side save, the client is (optionally) // updated with the server-side state. - const model = this; const success = options.success; const attributes = this.attributes; - options.success = function (resp) { + options.success = (resp) => { // Ensure attributes are restored during synchronous saves. - model.attributes = attributes; - let serverAttrs = options.parse ? model.parse(resp, options) : resp; + this.attributes = attributes; + let serverAttrs = options.parse ? this.parse(resp, options) : resp; if (wait) serverAttrs = { ...attrs, ...serverAttrs }; - if (serverAttrs && !model.set(serverAttrs, options)) return false; - if (success) success.call(options.context, model, resp, options); - model.trigger('sync', model, resp, options); + if (serverAttrs && !this.set(serverAttrs, options)) return false; + if (success) success.call(options.context, this, resp, options); + this.trigger('sync', this, resp, options); }; wrapError(this, options); @@ -269,19 +267,18 @@ export const Model = /*#__PURE__*/ (() => { // If `wait: true` is passed, waits for the server to respond before removal. destroy(options) { options = { ...options }; - const model = this; const success = options.success; const wait = options.wait; - const destroy = function () { - model.stopListening(); - model.trigger('destroy', model, model.collection, options); + const destroy = () => { + this.stopListening(); + this.trigger('destroy', this, this.collection, options); }; - options.success = function (resp) { + options.success = (resp) => { if (wait) destroy(); - if (success) success.call(options.context, model, resp, options); - if (!model.isNew()) model.trigger('sync', model, resp, options); + if (success) success.call(options.context, this, resp, options); + if (!this.isNew()) this.trigger('sync', this, resp, options); }; let xhr = false; diff --git a/src/sync.js b/src/sync.js index 21a14ffd5..a4ea93b61 100644 --- a/src/sync.js +++ b/src/sync.js @@ -82,7 +82,7 @@ export let sync = function sync(method, model, options) { // Pass along `textStatus` and `errorThrown` from jQuery. const error = options.error; - options.error = function (xhr, textStatus, errorThrown) { + options.error = (xhr, textStatus, errorThrown) => { options.textStatus = textStatus; options.errorThrown = errorThrown; if (error) error.call(options.context, xhr, textStatus, errorThrown);