brg
brg

Reputation: 3953

Emberjs 1.0.0-RC3: Can't use send to call controller method from view

Normally, we can access a method declared in the controller from the views using: this.get('controller').send('method');. In this simple jsfiddle, that is failing with: Uncaught TypeError: Cannot read property 'send' of undefined.

Here is the entire code in the jsfiddle.

 App = Ember.Application.create();

 App.YesController = Ember.ArrayController.extend({

   cow: function(){
       console.log('say mooooo') ;
    }  
 });

 App.YesView = Ember.View.extend({
    templateName: 'yes',
   um:  function(){    
     this.get('controller').send('cow');
   }
});

The entire view:

<script type='text/x-handlebars' data-template-name='application'>
  {{render yes}}
  {{outlet}}
</script>

<script type='text/x-handlebars' data-template-name='yes'>
   <h1> Can't use send to call controller method from view</h1>
    <button {{action 'um' target='view.content'}}> can't call controller from view</button>

  <button {{action 'cow'}}> controller action works</button>
</script>

Upvotes: 0

Views: 1013

Answers (2)

herom
herom

Reputation: 2542

As far as I know it's possible to call the cow() method in your controller like this:

App.YesView = Ember.View.extend({
  templateName: 'yes',
  um: function() {
    this.get('controller').cow();
    //this.controller.cow() // <-- this should work also
  }
})

Upvotes: 2

Martin Stannard
Martin Stannard

Reputation: 811

Try target=view rather that target=view.content, otherwise you're trying to call send on the model, which is undefined.

Upvotes: 1

Related Questions