Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
refactor: prefer this over bound this
Browse files Browse the repository at this point in the history
  • Loading branch information
Eli Skeggs committed Sep 8, 2020
1 parent 3c82e7f commit 879210c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
18 changes: 7 additions & 11 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
37 changes: 17 additions & 20 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 879210c

Please sign in to comment.