Skip to content

Commit

Permalink
refactor: convert js files to ts
Browse files Browse the repository at this point in the history
Workaround for privatenumber/tsx#38
But also, it's time for those to be TypeScript
Note, I added `// @ts-nocheck` to the top of the files to suppress
TypeScript errors for now, but this should be removed once we move way
from Backbone
  • Loading branch information
maxpatiiuk committed Feb 20, 2024
1 parent 353a0ea commit a3b51f7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 39 deletions.
38 changes: 0 additions & 38 deletions specifyweb/frontend/js_src/lib/components/DataModel/backbone.js

This file was deleted.

50 changes: 50 additions & 0 deletions specifyweb/frontend/js_src/lib/components/DataModel/backbone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// @ts-nocheck

import BackboneBase from 'backbone';
import $ from 'jquery';
import _ from 'underscore';

// REFACTOR: remove @ts-nocheck

// https://stackoverflow.com/questions/14866014/debugging-javascript-backbone-and-marionette

function createNamedConstructor(name, constructor) {
const function_ = new Function(
'constructor',
`return function ${name} () {\n` +
` // wrapper function created dynamically for "${name}"\n` +
` // constructor to allow instances to be identified in the debugger\n` +
` constructor.apply(this, arguments);\n` +
`};`
);
return function_(constructor);
}

const originalExtend = BackboneBase.View.extend;
const nameProperty = '__name__';
const newExtend = function (protoProps, classProps) {
if (protoProps && protoProps.hasOwnProperty(nameProperty)) {
// BUG: check that name is a valid identifier
const name = protoProps[nameProperty];
// Wrap constructor from protoProps if supplied or 'this' (thi function we are extending)
const constructor = protoProps.hasOwnProperty('constructor')
? protoProps.constructor
: this;
protoProps = _.extend(protoProps, {
constructor: createNamedConstructor(name, constructor),
});
} else {
console.warn('Creating backbone subclass without __name__ property.');
}
return originalExtend.call(this, protoProps, classProps);
};

BackboneBase.Model.extend =
BackboneBase.Collection.extend =
BackboneBase.Router.extend =
BackboneBase.View.extend =
newExtend;

BackboneBase.$ = $;

export { default as Backbone } from 'backbone';
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// @ts-nocheck

import _ from 'underscore';

import { assert } from '../Errors/assert';
import { Backbone } from './backbone';
import { hasHierarchyField } from './tables';

// REFACTOR: remove @ts-nocheck

const Base = Backbone.Collection.extend({
__name__: 'CollectionBase',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-nocheck

import _ from 'underscore';

import { hijackBackboneAjax } from '../../utils/ajax/backboneAjax';
Expand All @@ -18,6 +20,8 @@ import {
import { initializeResource } from './scoping';
import { specialFields } from './serializers';

// REFACTOR: remove @ts-nocheck

function eventHandlerForToOne(related, field) {
return function (event) {
const args = _.toArray(arguments);
Expand Down

0 comments on commit a3b51f7

Please sign in to comment.