I need a little help about ember-data record creation.
My app is a basic one, with books and reviews, based on Code School Tutorial
The app is using the RESTadapter
There is a form on the book page to write and send a review
<div class="row">
<div class="col-sm-09">
<h4>Reviews</h4>
{{#each review in reviews}}
<div class="col-sm-3">
<p>{{review.text}}</p>
<p class="text-info">{{review.reviewedDate}}</p>
</div>
{{else}}
<p class="text-muted">No Reviews Yet. Be the first to write one!</p>
{{/each}}
</div>
<div class="col-sm-3">
<h4>Review</h4>
{{textarea valueBinding='review.text'}}
<br>
<button {{action 'createReview'}} class='btn-primary'>Review</button>
{{#if review.text}}
<br><h4>Preview</h4>
<p class="text-muted">{{review.text}}</p>
{{/if}}
</div>
My Controller
App.BookController = Ember.ObjectController.extend({
logoAvailable: 'images/instock.jpg',
logoUnavailable: 'images/unavailable.jpg',
logoAvailability: function () {
if (this.get('isAvailable'))
return this.logoAvailable;
else
return this.logoUnavailable;
}.property('isAvailable'),
review: function () {
return this.store.createRecord('review', {
book: this.get('model')
});
}.property('model'),
actions: {
createReview: function () {
var controller = this;
this.get('review').save().then(function (review) {
controller.set('text', '');
controller.get('model.reviews').addObject(review);
}, function (error) {
console.error(error);
controller.set('text', '');
review.unloadRecord();
});
}
}
});
The controller creates a new "review" object for each opened book, and will eventualy save it when the button is pressed.
This is working, but
My uncommited record is shown in the Book reviews list, even before submiting it, (as if it was a live preview), and before the call of controller.get('model.reviews').addObject(review);
What's wrong with my code, and how can I only display commited records (sucessful save() call).
Aucun commentaire:
Enregistrer un commentaire