andyhmltn / ember-js-chatapp

MIT License
3 stars 1 forks source link

Make the 'Send' button work. #2

Open andyhmltn opened 11 years ago

andyhmltn commented 11 years ago

Currently the send button just submits a form to nowhere.

zspecza commented 11 years ago

The send button worked in mine - simple fix, (would fix it myself but Git is being iffy on my OS atm). Since the button is a <submit> (<button> defaults to type='submit') there are three fixes: change the <button>s type attribute to 'button' (<button type="button"), return false from the callback function in $('button').click() or pass an event parameter to the .click() method and then call event.preventDefault(). This will stop it from submitting the form. I like to combine all three strategies. Eg:

JavaScript:

$('button').click(function(event) {
    event.preventDefault();
    // code
    return false;
});

HTML:

<button type="button">Send!</button>
andyhmltn commented 11 years ago

Very true. Although, I'm just figuring out how to do it the ember way :p

andyhmltn commented 11 years ago

This seems to be the kind of thing that is needed: http://stackoverflow.com/questions/11985787/how-to-get-input-value-in-ember-js-app

zspecza commented 11 years ago

Interesting...

andyhmltn commented 11 years ago

Ahh that wasn't the one I saw earlier: http://stackoverflow.com/questions/8682172/trigger-an-action-in-a-text-field-from-a-button

zspecza commented 11 years ago

Derp. EmberJS's handlebars has an {{action}} helper that calls a method.

<script type="text/x-handlebars" data-template-name="message">
  <textarea></textarea>
  <button {{action "send"}}>Send!</button>
</script>
App.MessageController = Ember.ObjectController.extend({
  send: function() {
    socket.emit('sendmessage', options);
  }
});
zspecza commented 11 years ago

I really wish EmberJS had better docs... I'm resorting to listening to a podcast to find out more about action options.

andyhmltn commented 11 years ago

Sadly yes. Although, I have an idea how to do this. I just need to get around to doing it.