samedi 30 mai 2015

How can I delete a chat message with Emberfire?

I've created an action to add messages to my firebase, but can't seem to figure out how to remove a specific message. Here is my new message controller so far:

import Ember from 'ember';

export default Ember.Controller.extend({
//define actions here
actions: {
    addMessage: function() {
        //createRecord creates new instance of message model
        var newMessage = this.store.createRecord('message', {
            //this gets the info from the form fields with this value
            name: this.get('name'),
            body: this.get('body')
        });
        //this saves the data to store and firebase
        newMessage.save();
        //this resets the fields to be blank again
        this.setProperties({
            name: '',
            body: ''
        });
    }
  }
});

and this is my message controller with the remove action

import Ember from 'ember';

export default Ember.Controller.extend({
actions: {
    deleteMessage: function(id) {
        var msg = this.store.find('message', id, {
            //this gets the info from the form fields with this value
            name: this.get('name'),
            body: this.get('body')
        });
        //this deletes the data to store and firebase
        msg.remove();
    }
}
});

and here's my template

<div class="new-msg-link">
    {{#link-to 'messages.new'}}Add New Message{{/link-to}}
</div>

{{outlet}}

{{#each message in model}}
    <div class="each-msg">
        {{message.name}}: {{message.body}}
        <button {{action 'deleteMessage'}}>Delete</button>
    </div>
{{/each}}

I'm not sure how to pass the id parameter in my action correctly and how to get it from firebase correctly. Any suggestions would be great! thanks




Aucun commentaire:

Enregistrer un commentaire