I’m writing a small sample app using Ember.js. My goal is to determine use cases for Ember. My app is a simple Twitter feed viewer. I click an Ember.Button object which loads my Twitter feed and dumps it to the page. That all works great, but the username is hardcoded. I’m wanting to dig in deeper and so I’ve added an Ember.TextField view that I’d like to use to indicate the Twitter username.
I see how the button targets the bulleted list which displays the tweets, and I also see how it knows which controller to use to get the data. My question is how can I tell Ember to pull the username value from the input field and pass it into the method which calls for the tweets? I’m adding the existing JS code:
// app.js
var Tweets = Em.Application.create();
Tweets.tweetsArray = Em.ArrayController.create({
content: [],
loadTweets: function() {
var url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=commadelimited&callback=?';
$.getJSON(url,function(data){
Tweets.tweetsArray.pushObjects(data);
})
}
});
And here’s the view:
<script type="text/x-handlebars">
<div id="frm">
{{view Ember.TextField placeholder="Twitter username"}}
{{#view Ember.Button target="Tweets.tweetsArray" action="loadTweets"}}
Load Tweets
{{/view}}
</div>
<ul id="tweets">
{{#each Tweets.tweetsArray}}
<li>
<h3>{{user.screen_name}}</h3>
<p>{{text}}</p>
</li>
{{/each}}
</ul>
</script>
You’ll want to set username on an Ember.Object, like your app:
and then bind it to your TextField:
Ember watches change events on TextField and will keep the bound property updated.