Skip to content

Commit fad9f8f

Browse files
committed
refactor: conversion of functions to classes
1 parent c4b7d39 commit fad9f8f

File tree

1 file changed

+98
-89
lines changed
  • frameworks/keyed/knockout/src

1 file changed

+98
-89
lines changed

frameworks/keyed/knockout/src/Main.js

Lines changed: 98 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,100 @@
1-
2-
import ko from 'knockout';
3-
4-
var HomeViewModel = function () {
5-
var self = this;
6-
self.id = 1;
7-
8-
function _random(max) {
9-
return Math.round(Math.random() * 1000) % max;
1+
import ko from "knockout";
2+
3+
const adjectives = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", "cheap", "expensive", "fancy"];
4+
const colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"];
5+
const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"];
6+
7+
class HomeViewModel {
8+
constructor() {
9+
this.id = 1;
10+
this.selected = ko.observable(null);
11+
this.data = ko.observableArray();
12+
}
13+
14+
#random(max) {
15+
return Math.round(Math.random() * 1000) % max;
16+
}
17+
18+
buildData(count) {
19+
const data = [];
20+
for (let i = 0; i < count; i++) {
21+
data.push(
22+
new ItemViewModel(
23+
{
24+
id: this.id++,
25+
label: adjectives[this.#random(adjectives.length)] + " " + colours[this.#random(colours.length)] + " " + nouns[this.#random(nouns.length)],
26+
},
27+
this,
28+
),
29+
);
1030
}
11-
12-
function buildData(count) {
13-
var adjectives = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", "cheap", "expensive", "fancy"];
14-
var colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"];
15-
var nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"];
16-
var data = [];
17-
for (var i = 0; i < count; i++) {
18-
data.push(new ItemViewModel({
19-
id: self.id++,
20-
label: adjectives[_random(adjectives.length)] + " " + colours[_random(colours.length)] + " " + nouns[_random(nouns.length)]
21-
}, self));
22-
}
23-
return data;
31+
return data;
32+
}
33+
34+
run() {
35+
this.data(this.buildData(1000));
36+
this.selected(null);
37+
}
38+
39+
runLots() {
40+
this.data(this.buildData(10000));
41+
this.selected(null);
42+
}
43+
44+
add() {
45+
this.data.push.apply(this.data, this.buildData(1000));
46+
}
47+
48+
update() {
49+
const tmp = this.data();
50+
for (let i = 0; i < tmp.length; i += 10) {
51+
tmp[i].label(tmp[i].label() + " !!!");
2452
}
25-
26-
self.selected = ko.observable(null);
27-
self.data = ko.observableArray();
28-
29-
self.run = function () {
30-
self.data(buildData(1000));
31-
self.selected(null);
32-
};
33-
34-
self.runLots = function () {
35-
self.data(buildData(10000));
36-
self.selected(null);
37-
};
38-
39-
self.add = function () {
40-
self.data.push.apply(self.data, buildData(1000));
41-
};
42-
43-
self.update = function () {
44-
var tmp = self.data();
45-
for (let i = 0; i < tmp.length; i += 10) {
46-
tmp[i].label(tmp[i].label() + ' !!!');
47-
}
48-
};
49-
50-
self.clear = function () {
51-
self.data.removeAll();
52-
self.selected(null);
53-
};
54-
55-
self.swapRows = function () {
56-
var tmp = self.data();
57-
if (tmp.length > 998) {
58-
var a = tmp[1];
59-
tmp[1] = tmp[998];
60-
tmp[998] = a;
61-
self.data(tmp);
62-
}
63-
};
64-
65-
self.select = function (id) {
66-
self.selected(id);
67-
};
68-
69-
self.del = function (item) {
70-
var tmp = self.data();
71-
const idx = tmp.findIndex(d => d.id === item.id);
72-
self.data.splice(idx, 1);
73-
};
74-
};
75-
76-
var ItemViewModel = function (data, parent) {
77-
var self = this;
78-
79-
self.id = ko.observable(data.id);
80-
self.label = ko.observable(data.label);
81-
82-
self.del = function () {
83-
parent.del(self);
84-
};
85-
86-
self.select = function () {
87-
parent.select(self.id());
88-
};
89-
};
90-
91-
ko.applyBindings(new HomeViewModel(), document.getElementById('main'));
53+
}
54+
55+
clear() {
56+
this.data.removeAll();
57+
this.selected(null);
58+
}
59+
60+
swapRows() {
61+
const tmp = this.data();
62+
if (tmp.length > 998) {
63+
const a = tmp[1];
64+
tmp[1] = tmp[998];
65+
tmp[998] = a;
66+
this.data(tmp);
67+
}
68+
}
69+
70+
select(id) {
71+
this.selected(id);
72+
}
73+
74+
del(item) {
75+
const tmp = this.data();
76+
const idx = tmp.findIndex((d) => d.id === item.id);
77+
this.data.splice(idx, 1);
78+
}
79+
}
80+
81+
class ItemViewModel {
82+
constructor(data, parent) {
83+
this.id = ko.observable(data.id);
84+
this.label = ko.observable(data.label);
85+
this.parent = parent;
86+
}
87+
88+
del() {
89+
this.parent.del(this);
90+
}
91+
92+
select() {
93+
this.parent.select(this.id());
94+
}
95+
}
96+
97+
const main = document.getElementById("main");
98+
const homeView = new HomeViewModel();
99+
100+
ko.applyBindings(homeView, main);

0 commit comments

Comments
 (0)