http://www.wbotelhos.com/ratyの Jquery rate プラグインを使用しています。私は Rails + Backbone.js レビュー アプリを持っています。レビュー フォーム ビューを追加するたびに、星の上にカーソルを置いても星を「オン」にすることができません。プラグインは、読み取り専用の評価には機能しますが、評価の追加には機能しません。フォーム サブビューのコードは次のとおりです: //reviews_form.js
App.Views.ReviewsForm = Backbone.CompositeView.extend({
initialize: function(options){
this.business = options.business;
},
template: JST['reviews/form'],
events: {
"click #review-submit":"submitForm"
},
render: function(){
var renderedContent = this.template()
this.$el.html(renderedContent);
$inputRating = this.$el.find('#input-rating');
$inputRating.raty();
return this;
},
submitForm: function(event){
event.preventDefault();
this.model.reviews().create({
rating: $('#input-rating').val(),
content: this.$(".review_content").val(),
business_id: this.business.id
}, { wait: true });
$(".review_content").empty();
}
})
そして、次のコードを使用して、business_show.js ファイルにレビュー フォーム ビューを追加します。
addReviewForm: function(review){
var reviewsForm = new Expecto.Views.ReviewsForm({model: this.model, business: this.model});
this.addSubview(".reviews-form", reviewsForm)
},
私のフォームテンプレートは次のとおりです。
<h3 class="user-reviews">Add a review</h3>
<form class="form-horizontal" id="new-review-form">
<div class="form-group">
<div id="input-rating"></div>
<label>Review</label><br>
<textarea name="review[content]" class="review_content" style="width: 500px; height: 150px;"></textarea><br>
<input type="hidden" name="review[business_id]" value="<%= this.business_id %>">
<input type="hidden" name="review[user]" value="<%= this.user %>">
<button id="review-submit" class=" btn btn-primary">Submit</review>
</div>
</form>