简体   繁体   English

使用jQuery rateIt插件获取未捕获的TypeError

[英]Getting Uncaught TypeError with Jquery rateIt plugin

I am using Jquery rateIt plugin in my backbone project. 我在骨干项目中使用Jquery rateIt插件。

I am using its AJAX example part . 我正在使用其AJAX示例部件。

I could see images are loading no call going through and no response and some time get error like 我可以看到图像未加载任何呼叫,也没有响应,并且有些时候出现错误,例如

"Uncaught TypeError: Object rateAnswer has no method 'apply' " “未捕获的TypeError:对象rateAnswer没有方法'apply'”

here is my JS 这是我的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;
});

and HTML part is 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 这是RateIt插件示例的链接

The error message is typical of an event binding incorrectly done... (trying to call apply on something that isn't a function). 该错误消息是典型的事件绑定错误地完成的原因(尝试在不是函数的对象上调用apply)。

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

I have never seen this syntax, where did you see in the documentation that you can give a string instead of the function parameter? 我从未见过这种语法,在文档中您可以在何处给出字符串而不是function参数?

Here you want to bind to a function that is outside of your callback, so before the appCollectObj.fetch, add something like: 在这里,您想绑定到回调之外的函数,因此在appCollectObj.fetch之前,添加以下内容:

var thisView = this;

Then it should probably be: 然后可能应该是:

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

And you'll probably have issues because this in the event handler won't be working as you expect...so you can wrap your rateAnswer with $.proxy (or _.bindAll from _.underscore...): 而且您可能会遇到问题,因为事件处理程序中的该类将无法按您预期的方式工作...因此您可以用$ .proxy(或_.underscore中的_.bindAll ...)包装rateAnswer:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM