choojs / choo-handbook

πŸš‚βœ‹πŸ“– - Learn the choo framework through a set of exercises
https://handbook.choo.io
Other
268 stars 26 forks source link

sample app click behavior is weird #99

Closed agrw closed 6 years ago

agrw commented 6 years ago

for first choo app the clicking behavior seems odd, once you click on animal type , you have to randomly get a click on that animal, wouldn't it be better once having choosen an animal to now only click on that animal. change index.js , see **** new line commented code index.js: // import choo var choo = require('choo')

// import choo's template helper var html = require('choo/html')

// import template var main = require('./templates/main.js')

// initialize choo var app = choo()

app.use(function (state, emitter) { // initialize state state.animals = [ { type: 'lion', x: 200, y: 100 }, { type: 'crocodile', x: 50, y: 300 } ]

state.atype='all'

// add animal emitter.on('addAnimal', function (data) { var animals = ['crocodile', 'koala', 'lion', 'tiger', 'walrus'] var type = Math.floor(Math.random() * 5)

if (state.params.type)                                      //**** new line
  type=animals.indexOf(state.params.type);   //**** new line

var x = data.x
var y = data.y

var obj = { type: animals[type], x: x, y: y }
state.animals.push(obj)

emitter.emit('render')

})

// remove animal emitter.on('removeAnimal', function (i) { state.animals.splice(i, 1) emitter.emit('render') }) })

// declare routes app.route('/', main) app.route('/filter/:type', main)

// start app app.mount('div')

agrw commented 6 years ago

reissued to ouiscenter/choo-animals