-
Notifications
You must be signed in to change notification settings - Fork 1
/
on-js-frontend-frameworks.js
261 lines (211 loc) · 7.44 KB
/
on-js-frontend-frameworks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* BASIC COUNTER: */
// vanilla JS // // jquery: imperative - no build step //
<script> <script>
$(document).ready(function() {
var counter = 0; var counter = 0;
document.getElementById("app").innerHTML = counter; $('#app').html(counter) //show initial value of counter
function increment(){ $('#increment').click(function() {
counter++; counter++;
document.getElementById("app").innerHTML = counter; $('#app').html(counter)
} });
function decrement(){ $('#decrement').click(function() {
counter--; counter--;
document.getElementById("app").innerHTML = counter; $('#app').html(counter)
} });
})
</script> </script>
<h1 id="app"></h1> <h1 id="app"></h1>
<button onClick="increment()">+</button> <button id="increment">+</button>
<button onClick="decrement()">-</button> <button id="decrement">-</button>
// vue: declarative code - no build step //
<script>
new Vue({
el: '#app',
data: {
counter: 0
},
methods: {
increment() {this.counter++},
decrement() {this.counter--}
}
})
</script>
<div id="app">
<h1>{{ counter }}</h1>
<button @click="increment">+</button> // instead of calling a method we could simply say:
<button @click="decrement">-</button> // counter++ or counter--
</div>
// hyperapp: declarative and functional code /////
// implies Babel build step //////////////////////
const state = {
counter: 0
}
const actions = {
changeCounter: value => state => ({ counter: state.counter + value })
}
const view = (state, actions) => (
<div>
<h1>{state.counter}</h1>
<button onclick={() => actions.changeCounter(1)}>+</button>
<button onclick={() => actions.changeCounter(-1)}>-</button>
</div>
)
app(state, actions, view, document.getElementById('app'))
<div id="app"></div>
// react /////////////////////////////////////////
// implies Babel build step //////////////////////
class App extends React.Component {
constructor(props) {
super(props); //required
this.state = {counter: 0}
}
changeCounter(value) {
this.setState({counter: this.state.counter + value})
}
render() {return(
<div>
<h1>{ this.state.counter }</h1>
<button onClick={this.changeCounter.bind(this, 1)}>+</button>
<button onClick={this.changeCounter.bind(this, -1)}>−</button>
</div>
)}
}
ReactDOM.render(<App />, document.getElementById('app'))
<div id="app"></div>
/* CAPTURING USER INPUT: */
// jquery: imperative code - no build step ///////
<script> // with continuous user input capture
$(function() {
//keypress wouldn't include delete key, keyup does.
//We also query the div id app and find the other elements so that we can reduce lookups
$('#app').keyup(function(e) {
var userInput = $(this).find('#answerBox').val()
$(this).find('.answer').empty()
$(this).find('.answer').append(userInput)
})
})
</script>
<script> // with single event user input capture
$(function() {
$('#app').change(function(e) {
var userInput = $(this).find('#answerBox').val()
$(this).find('.answer').append(userInput)
})
})
</script>
<div id="app">
<label for="answerBox">Answer:</label>
<input id="answerBox" type="text" />
<p>Your answer is: <span class="answer"></span></p>
</div>
// vue: declarative code - no build step /////////
<script>
new Vue({
el: '#app',
data: {
answer: ''
}
})
</script>
<div id="app">
<label for="answer">Answer:</label>
<input id="answer" type="text" v-model="answer"/> // with countinuous user input capture
<input id="answer" type="text" v-model.lazy="answer"/> // with single user input capture
<p>Your answer is: <span>{{ answer }}</span></p>
</div>
/* HIDING AND SHOWING: */
// jquery: imperative code - no build step ///////
<script>
$(function() {
$('button').on('click', function() {
$('#hello').toggle() //$('#hello').toggleClass('red') would style the element
$(this).attr('aria-expanded', ($(this).attr('aria-expanded') == "false" ? true : false))
})
})
</script>
<div id="app">
<button aria-expanded="false">Toggle Panel</button>
<p id="hello">hello</p>
</div>
// vue: declarative code - no build step /////////
<script>
new Vue({
el: '#app',
data: {
active: false
}
})
</script>
<div id="app">
<button @click="active = !active" :aria-pressed="active ? 'true' : 'false'">Toggle me</button>
<p v-if="active">hello</p> <!-- If this button would work a lot it is preferable to use v-show instead
<p :class="{ red: active }">Sometimes I need to be styled differently</p> -->
</div>
/* HIDING AND SHOWING 2: */
// jquery: imperative code - no build step ///////
<script>
$(function() {
$('button').hide()
$('#textarea').keyup(function() {
if (textarea.val().length > 0) {
$('button').show()
} else {
$('button').hide();
}
})
})
</script>
<div id="app">
<label for="textarea">What is your favorite kind of taco?</label>
<textarea id="textarea"></textarea>
<button>Let us know!</button>
</div>
// vue: declarative code - no build step /////////
<script>
new Vue({
el: '#app',
data() {
return {
tacos: ''
}
}
})
</script>
<div id="app">
<label for="textarea">What is your favorite kind of taco?</label>
<textarea id="textarea" v-model="tacos"></textarea>
<button v-show="tacos">Let us know!</button>
</div>
// How jquery work..
$('one string! describing the elements you want to select').jqueryMethod().pipingMethodsPossible()
// HACKERRANK exercise
// Return a count of the total number of objects 'o' satisfying o.x == o.y.
// Parameter: an array of objects with integer properties 'x' and 'y'
// solution jsArray.filter
function getCount(objects) {
return objects.filter(object => object.x == object.y).length;
}
// solution for .. of
function getCount(objects) {
let count = 0
for (let o of objects) {
if (o.x == o.y) count++
}
return count
}
// solution for .. ín
function getCount(objects) {
let count = 0;
for (let o in objects) {
if (objects[o].x == objects[o].y) count++
}
return count
}
// solution jsArray.forEach
function getCount(objects) {
let count = 0;
objects.forEach(function (o) {
if (o.x == o.y) count++
})
return count
}