"Length" in the Guides vs Reality


App.Router.map(function() {
  this.resource('post', { path: '/post/:post_id' });

App.PostsRoute = Ember.Route.extend({
  afterModel: function(posts, transition) {
    if (posts.length === 1) {
      this.transitionTo('post', posts[0]);

For me, in 100% of all cases, it has to be:

if (posts.get('length') === 1) { 

posts.length is always undefined

Is this a mistake in the official docs?


I will second this observation. Just ran into the exact same thing, had to use .get() for the length.

change the docs then, send a PR

1 Like

Pull request filed here: https://github.com/emberjs/website/pull/1335

Whether or not the length property exists depends on what type of collection you’re using. If your model is a native array (that has Ember extensions added in), then the length property will be accurate. But if you’re using an Ember class, like a PromiseArray, then length isn’t a property on the object itself, rather a computed property that could be proxying to another object. And since most of the Ember guides work without the assumption of Ember-Data, I would imagine that particular example is using a native array.

However, I personally like to always use Em.get(posts, 'length') regardless of the object type, as it protects me from null references.