mjpwall
mjpwall

Reputation: 106

emberjs call action from onclick

I have a model which will return a block of text, I want an alert that will define particular words within that block of text. So I want to add a link next to the particular word that will call an action that creates the alert.

I can't use handlebars action method as the page will already be rendered. So when I'm pushing my model I want to include something like this:

<button id='definition' onclick='newAlert()'>i</button>

There may be a number of words that need the definition in a single block of text so I'd need to send an identifier also.

My question is, is it possible to call an action in this way?

Thanks for any help.

Upvotes: 2

Views: 5800

Answers (1)

melc
melc

Reputation: 11671

Whenever it is required to access ember entities from the DOM using pure js, for example when required to handle an onclick js event and delegate it to ember entities, it is possible to do something like

http://emberjs.jsbin.com/mimuwale/1/edit

js

App = Ember.Application.create();

function getView($el){
  return Ember.View.views[$el.closest(".ember-view").attr("id")];
}

function newAlert(el){
  getView($(el)).get('controller').send('newAlert',el.id);
}

App.IndexView=Ember.View.extend();

App.IndexController=Ember.Controller.extend({
  actions:{
    newAlert:function(buttonId){alert('clicked button:'+buttonId);}
  }
});

hbs

<script type="text/x-handlebars" data-template-name="index">
  <button id='definition' onclick='newAlert(this)'>test</button>
</script>

Upvotes: 1

Related Questions