How to show immediately a property of an associated model after creation?

117 views Asked by At

If I create a new movie object, the associated username from owner (App.User) isn't shown. It shows only after I reload the page. Any idea how I can achieve to show immediately the associated username after I have created a new movie?

Code so far:

App.Movie = Ember.Model.extend({
  objectId: Ember.attr(),
  title: Ember.attr(),
  year: Ember.attr(),

  owner: Ember.belongsTo('App.User', {
    key: 'owner',
    serializer: UserType
  })
});

App.User = Ember.Model.extend({
  objectId: Ember.attr(),
  username: Ember.attr(),
});

App.Movie.adapter = Ember.Adapter.create({
  createRecord: function(record) {
    return Ember.$.ajax({
      headers: {
        'X-Parse-Application-Id': '',
        'X-Parse-REST-API-Key': ''
      },
      type: 'POST',
      url: 'https://api.parse.com/1/classes/Movie',
      contentType: 'application/json',
      data: JSON.stringify(record)
    }).then(function(data) {
      record.load(data.objectId, record.get('_data'));
      record.didCreateRecord();
    });
  }
});

{{#each movie in this}}
  <tr>
    <td>{{movie.year}}</td>
    <td>{{movie.title}}</td>
    <td>{{movie.owner.username}}</td>
  </tr>
{{/each}}

App.MoviesIndexController = Ember.ArrayController.extend({
  rawDescription: '',
  year: '',
  title: '',

  errors: null,

  actions: {
    createMovie: function () {
      var rawDescription = this.get('rawDescription');

      if (!rawDescription.match(/([^$]+)(\d{4})/)) {
        this.set('errors', {
          rawDescription: 'Oh snap! Please include the movie\'s title and year.'
        });
      } else if (!this.isUnique({
        rawDescription: rawDescription
      })) {
        this.set('errors', {
          rawDescription: 'Oh snap! The movie already exists.'
        });
      } else {
        var rv = this.parseRawDescription(this.get('rawDescription')),
          title = rv[1],
          year = rv[2],
          newMovie = App.Movie.create({
            owner: App.Session.authUser,
            ratings: [{ objectId: App.Session.objectId, value: 0 }]
            title: title,
            watched: false,
            year: year,
          });

        newMovie.save();

        this.setProperties({ rawDescription: '', errors: null });
      }
    }
  }
});
1

There are 1 answers

5
Kingpin2k On

I'm showing it as working, are you sure App.Session.authUser is populated? if you try console.log(newMovie.get('owner.username')) after you create it does it show anything?

http://emberjs.jsbin.com/AYidAdi/1/edit