Skip to content

Commit f75cd83

Browse files
committed
Add new slides
1 parent 0dc2a3f commit f75cd83

29 files changed

+483
-133
lines changed

js-decorators/demos/.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["env"],
3+
"plugins": ["transform-decorators-legacy"]
4+
}

js-decorators/demos/angular-like.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const COMPONENTS = [];
2+
3+
const bootstrap = function() {
4+
COMPONENTS.forEach(component => {
5+
document.querySelectorAll(component.selector).forEach(node => {
6+
const instance = new component.class();
7+
node.innerHTML = component.template(instance);
8+
});
9+
});
10+
};
11+
12+
function Component(config) {
13+
config.template = _.template(config.template);
14+
return function(target) {
15+
config.class = target;
16+
COMPONENTS.push(config);
17+
}
18+
}
19+
20+
@Component({
21+
selector: 'minsk-js',
22+
template: `
23+
<h1>Hello, MinskJS #<%= number %>!</h1>
24+
`
25+
})
26+
class MinskJsComponent {
27+
constructor(number) {
28+
this.number = number;
29+
}
30+
}
31+
32+
bootstrap();

js-decorators/demos/index.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function fluent(target, name, descriptor) {
2+
const fn = descriptor.value;
3+
descriptor.value = function(...args) {
4+
fn.apply(target, args);
5+
return target;
6+
}
7+
}
8+
9+
function decorateWith(decorator) {
10+
return (target, name, descriptor) => {
11+
descriptor.value = decorator.call(target, descriptor.value);
12+
}
13+
}
14+
15+
function describe(description) {
16+
return (target, name) => {
17+
target.description = target.description || {};
18+
target.description[name] = description;
19+
};
20+
}
21+
22+
class Person {
23+
@fluent
24+
@describe('Set the name of person')
25+
setName(first, last) {
26+
this.first = first;
27+
this.last = last;
28+
}
29+
30+
@fluent
31+
sayName() {
32+
console.log(this.first, this.last);
33+
}
34+
}
35+
36+
const p = new Person();
37+
p.setName('Jane', 'Doe').sayName().setName('John', 'Doe').sayName();
38+
39+
const desc = Person.prototype.description.setName;
40+
console.log(desc);

js-decorators/demos/out.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict';
2+
3+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4+
5+
var _dec, _desc, _value, _class;
6+
7+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8+
9+
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
10+
var desc = {};
11+
Object['ke' + 'ys'](descriptor).forEach(function (key) {
12+
desc[key] = descriptor[key];
13+
});
14+
desc.enumerable = !!desc.enumerable;
15+
desc.configurable = !!desc.configurable;
16+
17+
if ('value' in desc || desc.initializer) {
18+
desc.writable = true;
19+
}
20+
21+
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
22+
return decorator(target, property, desc) || desc;
23+
}, desc);
24+
25+
if (context && desc.initializer !== void 0) {
26+
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
27+
desc.initializer = undefined;
28+
}
29+
30+
if (desc.initializer === void 0) {
31+
Object['define' + 'Property'](target, property, desc);
32+
desc = null;
33+
}
34+
35+
return desc;
36+
}
37+
38+
function fluent(target, name, descriptor) {
39+
var fn = descriptor.value;
40+
descriptor.value = function () {
41+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
42+
args[_key] = arguments[_key];
43+
}
44+
45+
fn.apply(target, args);
46+
return target;
47+
};
48+
}
49+
50+
function decorateWith(decorator) {
51+
return function (target, name, descriptor) {
52+
descriptor.value = decorator.call(target, descriptor.value);
53+
};
54+
}
55+
56+
function describe(description) {
57+
return function (target, name) {
58+
target.description = target.description || {};
59+
target.description[name] = description;
60+
};
61+
}
62+
63+
var Person = (_dec = describe('Set the name of person'), (_class = function () {
64+
function Person() {
65+
_classCallCheck(this, Person);
66+
}
67+
68+
_createClass(Person, [{
69+
key: 'setName',
70+
value: function setName(first, last) {
71+
this.first = first;
72+
this.last = last;
73+
}
74+
}, {
75+
key: 'sayName',
76+
value: function sayName() {
77+
console.log(this.first, this.last);
78+
}
79+
}]);
80+
81+
return Person;
82+
}(), (_applyDecoratedDescriptor(_class.prototype, 'setName', [fluent, _dec], Object.getOwnPropertyDescriptor(_class.prototype, 'setName'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'sayName', [fluent], Object.getOwnPropertyDescriptor(_class.prototype, 'sayName'), _class.prototype)), _class));
83+
84+
85+
var p = new Person();
86+
p.setName('Jane', 'Doe').sayName().setName('John', 'Doe').sayName();
87+
88+
var desc = Person.prototype.description.setName;
89+
console.log(desc);

js-decorators/demos/react-bind.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@autobind
2+
class AddNewItemComponent extends React.Component {
3+
constructor(props) {
4+
super(props);
5+
this.state = {
6+
items: []
7+
};
8+
}
9+
10+
addItem(event) {
11+
event.preventDefault();
12+
const item = this.refs.item.value.trim();
13+
if (item !== '') {
14+
this.setState((prevState) => ({
15+
items: [...prevState.items, item]
16+
}));
17+
}
18+
}
19+
20+
render() {
21+
return (
22+
<div>
23+
<h3>Add new item</h3>
24+
{/* <form className='new-item' onSubmit={this.addItem.bind(this)}> */}
25+
<form className='new-item' onSubmit={this.addItem}>
26+
<input type='text' ref='item' />
27+
<button type='submit'>Add</button>
28+
</form>
29+
</div>
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)