2013-12-11 91 views
0

我在我的骨干项目中使用Jquery rateIt插件。Jquery rateIt插件不工作

我正在使用它的AJAX示例部分。

我能看到图像加载没有呼叫经历和没有任何反应,一段时间后得到错误像

“遗漏的类型错误:对象rateAnswer有没有方法‘应用’”

这里是我的JS

define(['jquery', 'underscore', 'backbone', 'text!tpl/questionnaire.html', 'Collection/cAppCollection', 'Models/mAppModel', 'jqueryRateIt'], 
function($, _, Backbone, questionnaireTpl, appCollection, appModel, jqRateIt) { 

var questionnaire = Backbone.View.extend({ 

    el : '.row', 
    template : _.template(questionnaireTpl), 

    initialize : function(e) { 

     $('.hero-unit').css('display','none'); 
     this.render(e); 
    }, 
    //this renders my template 
    //on success of response , loads response than call rateit() function than bind rated and reset to function rateAnswer 
    //but here no call fo rateAnswer 
    render : function(e) { 

      var elem = this.$el, 
       temp = this.template, 
       appCollectObj = new appCollection(); 

       appCollectObj.fetch({ 
        data: $.param({subject: e.id}), 
        success: function(model, response) { 
          $(elem).html(temp({model:response})); 
          $('div.rateit').rateit(); 
          $('#products .rateit').bind('rated reset', 'rateAnswer'); 
          }, 
        error: function() { 
          console.log('Failed to fetch!'); 
          } 
       }); 

    }, 

    rateAnswer : function(){ 

       var ri = $(this), 
       appCollectObj = new appCollection(); 


       var value = ri.rateit('value'); 
       var questionId = ri.data('productid'); 

       appCollectObj.fetch({ 
        data: $.param({questionId : questionId, value : value}), 
        success: function(model, response) { 
          $('#response').append('<li>' + data + '</li>'); 
          }, 
        error: function() { 
          $('#response').append('<li style="color:red">' + msg + '</li>'); 
          } 
       }); 
    } 
    }); 

    return questionnaire; 
}); 

和HTML部分是

<div id="products"> 
     <ul> 
      <li> 
      RateIt: <div data-productid="<%= elem.id %>" class="rateit"></div> 
      </li> 
     </ul> 
     <ul id="response"></ul> 
     </div> 

here the link to RateIt plugin example

回答

1

错误消息是一个典型的事件绑定不正确完成...(试图调用适用于非函数的东西)。

$('#products .rateit').bind('rated reset', 'rateAnswer'); 

我从来没有见过这样的语法,你在文档中看到你可以给一个字符串而不是函数参数?

在这里,你要绑定到一个函数,你的回调之外,所以appCollectObj.fetch之前,添加类似:

var thisView = this; 

那么它可能应该是:

$('#products .rateit').bind('rated reset', thisView.rateAnswer); 

而且你可能会遇到问题,因为事件处理程序中的这个将不会按照你的预期工作......所以你可以用$ .proxy(或_.underscore中的_.bindAll ...)打包你的rateAnswer:

$('#products .rateit').bind('rated reset', $.proxy(thisView.rateAnswer, thisView)); 
+0

尝试$ .proxy(this.rateAnswer,this)以及$ .proxy(this.rateAnswer,this)仍然没有调用函数rateAnswer – mayank

+0

$('#products .rateit')。bind('rated reset' ,'rateAnswer');在这里传递字符串而不是函数是我的不好的感谢修正。现在我没有错误,但没有呼吁绑定功能 – mayank

+0

啊,我的坏,我做了太多的咖啡标记(和它美丽的胖箭头)。你已经在回调,所以'这'已经'错误'了。我编辑了我的答案。 – Enders