0

我正在使用来自http://www.wbotelhos.com/raty的 Jquery raty 插件。我有一个 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>
4

1 回答 1

0

I believe you actually have to pass in a callback that raty will use onClick. From the raty docs.

$('div').raty({
  click: function(score, evt) {
  alert('ID: ' + this.id + "\nscore: " + score + "\nevent: " + evt);
  }
});

So in your case just put a function that set's the value of a rating to score this.val(score) .

于 2015-03-20T01:57:14.960 回答