Access content inside a controller function ember

I have a route, controller, view like this. The problem is I called controller function reloadTime from view but in reloadTime function I console the content for this controller but it says it is undefined. My question is how to access this content in ember?

App.ActivecallsRoute = Ember.Route.extend({
    setupController:function(controller,model){
        $.ajax({
            url:'requests/activecalls.php',
            type:'POST',
            success:function(data){
                App.Cdrglobal.set('active_call',data.length);
                controller.set('content',data);
            }
        })
    }
});

App.ActivecallsController = Ember.ArrayController.extend({
    content:[],
    deleteCall:function(model){
       var obj = this.findProperty('ID',model.ID);
       App.Cdrglobal.set('active_call',App.Cdrglobal.active_call-1);
       this.removeObject(obj);
    },
    reloadTime:function(){
        console.log(this.get('content'));//console undefined
        console.log(this.content);console undefined
    }
});


App.ActivecallsView = Ember.View.extend({
   didInsertElement:function(){
       this.get('controller').reloadTime();
   }
});

Upvotes: 0

Views: 2799

Answers (1)

Teddy Zeenny
Teddy Zeenny

Reputation: 3971

You are accessing the content property correctly. The reason why you are getting undefined is because the content property is actually undefined.

Now the reason why your content is undefined, is because Ember.js automatically sets the controller's content to the return value of the model hook in the route.

Since you didn't define a model method, the return value if this hook is undefined and therefore Ember.js is setting the controller content property to undefined.

Solution:

Create a dummy model hook that just returns an empty array:

App.ActivecallsRoute = Ember.Route.extend({
  setupController:function(controller,model){
    $.ajax({
      url:'requests/activecalls.php',
      type:'POST',
      success:function(data){
        App.Cdrglobal.set('active_call',data.length);
        controller.set('content',data);
      }
    });
  },
  model: function() {
    return [];
  }
});

Upvotes: 3

Related Questions