commadelimited
commadelimited

Reputation: 5119

Getting the text of Ember.Button within the click method

I'm trying to find out the "Ember" way of getting the text label of an Ember.Button from within the click handler. Currently I have this (and it works) but I suspect there's a more "correct" way to do it:

App.RecentNameBtn = Em.Button.extend({
    click: function(e){
        App.tweetsArray.set('username', e.srcElement.innerText);
        App.tweetsArray.loadTweets();
    }
});

Upvotes: 0

Views: 1623

Answers (1)

pangratz
pangratz

Reputation: 16163

There is a discussion whether Ember.Button should be deprecated. Regardless of that, I would create an App.userController which holds the username and use the {{action}} helper in your view, see http://jsfiddle.net/bZ8fY/. It may look like an overhead but by using bindings you are much more flexible. I'd also suggest to move the logic out of the view into a controller.

Handlebars:

<script type="text/x-handlebars" >
    {{#view App.TweetView}}
        <button {{action "loadTweets" target="App.userController"}}>
            {{username}}
        </button>
    {{/view}}
</script>​

JavaScript:

App = Ember.Application.create({});

App.userController = Ember.Object.create({
    username: 'emberjs',
    loadTweets: function(evt) {
        console.log( this.get('username') );
    }
});

App.TweetView = Ember.View.extend({
    usernameBinding: 'App.userController.username'
});
​

Upvotes: 3

Related Questions