brg
brg

Reputation: 3953

emberjs controller's needs returns undefined and unable to access controller.content

I am trying to use Emberjs needs api* to access Postscontroller from a comments controller. The PostController is backed by a route while I don't want the comment's controller to have a route.

In the comments controller, I have needs: ['posts', 'postsShow']. From the comments controller, when I run console log with the following commands,:

    console.log( this.get('controllers.postsShow') );

    console.log( this.get('controllers.posts') );

In the console I see:

<EmBlog.PostsShowController:ember396> { target=<EmBlog.Router:ember316>, namespace=EmBlog, store=<EmBlog.Store:ember336> 

 <EmBlog.PostsController:ember304> { target=<EmBlog.Router:ember316>, namespace=EmBlog, store=<EmBlog.Store:ember336>

However, when I try to access the controller content for PostsShowController or PostsController, it always returns post undefined. These are various approaches I have tried and still got post undefined:

 var post = this.get('controllers.posts').get('content');

              or

var post =  this.get('controllers.posts.content');

Also I tried to get 'comments' from the content like this:

var post = this.get('controllers.posts')

var comment = post.get('comments');

          or

 comment = post.comments;

I still got the error:

 TypeError: post is undefined  comment = post.comments;

 TypeError: post is undefined var comment = post.get('comments');

Which also means:

  var post =  this.get('controllers.posts.model').get('store.transaction');

  also returns post is  undefined.

This is the jsfiddle and the relevant section of the code is pasted below:

    EmBlog.PostsNewController = Ember.ObjectController.extend({
       content: null
    });

    EmBlog.PostsShowController =
       Ember.ObjectController.extend({
       content: null
    });


   EmBlog.CommentNewController = Em.ObjectController.extend({

       needs: ['posts', 'postsShow'],    
       isAddingNew: false,

      addComment: function(body){

           console.log( this.get('controllers.postsShow') );

           console.log( this.get('controllers.posts') );

           var post =  this.get('controllers.posts.content');
               store = post.get('store.transaction');

      }

  });

Many thanks

Upvotes: 4

Views: 3273

Answers (1)

Teddy Zeenny
Teddy Zeenny

Reputation: 3971

That's because posts controller is empty. You are filling the posts in PostIndexController, not PostsController.

Check the route:

EmBlog.PostsRoute  = Ember.Route.extend({
});

EmBlog.PostsIndexRoute  = Ember.Route.extend({
  model: function(){
    return EmBlog.Post.find();
  },
  setupController: function(controller, model){
    controller.set('content', model);
  }
});

So you should either do

needs:  ['postsIndex', 'postsShow']

and then:

this.get('controllers.postsIndex.content')

or fix your route:

EmBlog.PostsRoute  = Ember.Route.extend({
  model: function() {
    return EmBlog.Post.find();
  }
});

EmBlog.PostsIndexRoute  = Ember.Route.extend({
  model: function(){
    return this.modelFor('posts');
  },
  setupController: function(controller, model){
    controller.set('content', model);
  }
});

Updated fiddle

Upvotes: 6

Related Questions