The way I see it you have two options and it comes down to if you want this state to be persisted.
Option 1
I’m not sure what the nature of isDone
is, but it sounds like it could be a property on the model that is saved to the server. If that’s the case, you would do something like:
View:
{{!-- templates/my-view.hbs --}}
{{#each tweet as |model|}}
<div>{{model.text}}</div>
<button class="btn btn-default" {{action "reTweet" model}} disabled={{model.isDone}}>Retweet</button>
</div>
{{/each}}
Controller:
// controllers/my-controller.js
export default Ember.Controller.extend({
isDone: false,
actions: {
reTweet: function(model) {
model.set('isDone', true);
model.save();
}
}
});
In this case you would send the model as an argument to your action. The action would toggle your property and save it to the server.
Option 2:
If isDone
is really just view state and it doesn’t have to persist, then you should make a component to house the state.
View:
{{!-- templates/my-view.hbs --}}
{{#each tweet as |model|}}
{{#tweet-message model=model as |tweetMsg|}}
<div>{{model.text}}</div>
<button class="btn btn-default" {{action "reTweet" target=tweetMsg}} disabled={{tweetMsg.isDone}}>Retweet</button>
</div>
{{/tweet-message}}
{{/each}}
Component js:
// components/tweet-message.js
export default Ember.Component.extend({
isDone: false,
actions: {
reTweet: function() {
this.set('isDone', true);
}
}
});
Lastly, component template:
{{!-- templates/component/tweet-message.hbs --}}
{{yield this}}
So in this example, we’re create a “helper component” that houses the isDone
state. You’ll see in the last code snippet that the component yields itself ({{yield this}}
). If you don’t know, that yielded value will end up as the tweetMsg
value in the first snippet ({{#tweet-message model=model as |tweetMsg|}}
). This yielding is the connective tissue. It gives your view template a reference to the component so you can access it’s state and send actions.
IMHO this option is the “ember way”.