@@ -215,37 +215,35 @@ var watchExampleVM = new Vue({
215215 // whenever question changes, this function will run
216216 question : function (newQuestion , oldQuestion ) {
217217 this .answer = ' Waiting for you to stop typing...'
218- this .getAnswer ()
218+ this .debouncedGetAnswer ()
219219 }
220220 },
221- methods : {
221+ created : function () {
222222 // _.debounce is a function provided by lodash to limit how
223223 // often a particularly expensive operation can be run.
224224 // In this case, we want to limit how often we access
225225 // yesno.wtf/api, waiting until the user has completely
226226 // finished typing before making the ajax request. To learn
227227 // more about the _.debounce function (and its cousin
228228 // _.throttle), visit: https://lodash.com/docs#debounce
229- getAnswer: _ .debounce (
230- function () {
231- if (this .question .indexOf (' ?' ) === - 1 ) {
232- this .answer = ' Questions usually contain a question mark. ;-)'
233- return
234- }
235- this .answer = ' Thinking...'
236- var vm = this
237- axios .get (' https://yesno.wtf/api' )
238- .then (function (response ) {
239- vm .answer = _ .capitalize (response .data .answer )
240- })
241- .catch (function (error ) {
242- vm .answer = ' Error! Could not reach the API. ' + error
243- })
244- },
245- // This is the number of milliseconds we wait for the
246- // user to stop typing.
247- 500
248- )
229+ this .debouncedGetAnswer = _ .debounce (this .getAnswer , 500 )
230+ },
231+ methods: {
232+ getAnswer : function () {
233+ if (this .question .indexOf (' ?' ) === - 1 ) {
234+ this .answer = ' Questions usually contain a question mark. ;-)'
235+ return
236+ }
237+ this .answer = ' Thinking...'
238+ var vm = this
239+ axios .get (' https://yesno.wtf/api' )
240+ .then (function (response ) {
241+ vm .answer = _ .capitalize (response .data .answer )
242+ })
243+ .catch (function (error ) {
244+ vm .answer = ' Error! Could not reach the API. ' + error
245+ })
246+ }
249247 }
250248})
251249 </script >
@@ -273,28 +271,28 @@ var watchExampleVM = new Vue({
273271 watch: {
274272 question : function (newQuestion , oldQuestion ) {
275273 this .answer = ' Waiting for you to stop typing...'
276- this .getAnswer ()
274+ this .debouncedGetAnswer ()
277275 }
278276 },
277+ created : function () {
278+ this .debouncedGetAnswer = _ .debounce (this .getAnswer , 500 )
279+ },
279280 methods: {
280- getAnswer: _ .debounce (
281- function () {
282- var vm = this
283- if (this .question .indexOf (' ?' ) === - 1 ) {
284- vm .answer = ' Questions usually contain a question mark. ;-)'
285- return
286- }
287- vm .answer = ' Thinking...'
288- axios .get (' https://yesno.wtf/api' )
289- .then (function (response ) {
290- vm .answer = _ .capitalize (response .data .answer )
291- })
292- .catch (function (error ) {
293- vm .answer = ' Error! Could not reach the API. ' + error
294- })
295- },
296- 500
297- )
281+ getAnswer : function () {
282+ if (this .question .indexOf (' ?' ) === - 1 ) {
283+ this .answer = ' Questions usually contain a question mark. ;-)'
284+ return
285+ }
286+ this .answer = ' Thinking...'
287+ var vm = this
288+ axios .get (' https://yesno.wtf/api' )
289+ .then (function (response ) {
290+ vm .answer = _ .capitalize (response .data .answer )
291+ })
292+ .catch (function (error ) {
293+ vm .answer = ' Error! Could not reach the API. ' + error
294+ })
295+ }
298296 }
299297})
300298</script >
0 commit comments