Ember - didLoad while waiting for the ember-data model to load

hi I have the following route:

MB3.PlaylistRoute = Ember.Route.extend({
  model: function(params) {
    return MB3.Playlist.find(params.playlist_id);
  }
});

      

The playlist has a hasMany realtion with tracks. in the playlist view i want to do some logic with the attribute of the first track of the playlist.

so I added this code:

MB3.PlaylistView = Ember.View.extend({
  didInsertElement: function() {
    console.log(this.get("controller.tracks").objectAt(0).get("title"));
  }
});

      

The problem is the undefined header (I think because it hasn't been loaded yet. The second thing I tried is waiting for the didLoad event:

MB3.PlaylistView = Ember.View.extend({
      didInsertElement: function() {
        var self=this;
        this.get("controller.tracks").on("didLoad", function() {
          console.log(self.get("controller.tracks").objectAt(0).get("title"));
        });
      }
    });

      

but this also leads to a null value. How to do it?

+3


source to share


1 answer


As per Adrien's comments, it seems that you are running into issue 587 . However, I don't think you really need the "didLoad" callback in this case. Try using a computed property to get the video_id or track title instead. For example:

MB3.PlaylistView = Ember.View.extend({
  firstTrackTitle: function() {
    return this.get('controller.tracks.firstObject.title');
  }.property('controller.tracks.firstObject.title')      
});

      

Then in your template insert the player if this property is defined:



{{#if view.firstTrackTitle}}
    embed code here
{{/if}}

      

FWIW I would put this logic in a controller instead of a view, but the same idea.

+3


source







All Articles