0

I'm using Firebase as the backend for my Ember app and successfully hooked up Firebase with EmberFire. However, when I try to save my models after creating a record, while they are posted to Firebase, they are not updated in the client's ember template.

My code for the action handler before I realized this was:

actions: {
    publishPost: function() {
      var newPost = this.store.createRecord('post', {
        title: this.get('post.title'),
        body: this.get('post.body'),
        timestamp: new Date()
      });

      newPost.save();

    }
 }

And my code to try and solve this (but that doesn't work) by catching the promise returned by the save() and find() methods is:

actions: {
    publishPost: function() {
      var newPost = this.store.createRecord('post', {
        title: this.get('post.title'),
        body: this.get('post.body'),
        timestamp: new Date()
      });
      this.store.find('post').then(function(posts) {
        posts.addObject(newPost);
        posts.save().then(function() {
          newPost.save();
        });
      });

    }
}

Am I just not handling the logic in the above code correctly to update the template or is there another Firebase/EmberFire compatible solution for this?

mmwebster
  • 152
  • 10

1 Answers1

1

On https://github.com/broerse/ember-cli-blog-fire I did this:

import Ember from "ember";

export default Ember.ObjectController.extend({
  actions: {
    createPost: function() {
      var newPost = this.get('store').createRecord('post');
      newPost.set('date' , new Date());
      newPost.set('author' , 'C.L.I. Ember');
      this.get('target').transitionTo('post', newPost.save());
    }
 }

});

transitionTo can handle the newPost.save() promise.

Try it on: http://exmer.com/bloggrfire/posts