【问题标题】:Backbone.js - Solving methodBackbone.js - 求解方法
【发布时间】:2017-08-18 06:37:59
【问题描述】:

BACKBONE 和 Jquery

我有一段代码:

    var ChatMessage = Backbone.View.extend({
    el : '#friend-list',
    events : {},
    initialize : function() {},
    loadMessage  : function(parent_id) {
        //ukrycie komunikatów etc.
        $("#chat_body").html('');
        $("#end-record-info").hide();
        $("#end-record-load").hide();
        page = 1;
        this.ajax(parent_id,page); //initial data load
    },

    ajax : function(parent_id,page) {
        $.getJSON( "/**/**/"+parent_id+"/"+page, function( json ) {
            $("#end-record-load").hide();
            this.build(json.message, page);
            page ++; //page increment
            loading = false;  //set loading flag off
            end_record = false;
            if(json.max < page){ //no more records
                end_record = true; //set end record flag on
                return; //exit
            }
        });
    },

    build : function(arr, page) {
        alert('dgd');
        html = '';
        var height = 0;
        arr.reverse();
        $.each(arr, function(i, data){
            html += '<div class="answer '+data.side+'">';
            html += '<div class="avatar">';
            html += '<a href="**" target="_blank">';
            html += ''+data.id+'';
            html += '</a>';
            html += '<div class="status offline"></div>';
            html += '</div>';
            html += '<div class="name">';
            html += '<a href="**" target="_blank">';
            html += data.username;
            html += '</a>';
            html += '</div>';
            html += '<div class="text">';
            html += data.content;
            html += '</div>';
            html += '<div class="time">';

            if (data.side != 'left') {
                html += data.dateSendMessage
            }
            else {
                if (data.read == 0) {
                    html += 'xxx';
                }
                else {
                    html += 'xxx';
                }
            }
            html += '</div>';
            html += '</div>';
        });

        var nh = $('#chat_body').html();
        $('#chat_body').html(html+nh);

        if (page == 1) {
            $('#chat_body .answer').each(function(i, value){
                height += parseInt($(this).height());
            });

            height += '';
            $('.chat2').scrollTop(height);
        }
    }
});

在 AJAX 方法中我想构建方法

  this.build(json.message, page);

但是有些东西不起作用,我得到一个错误。

pm2.js:67TypeError: this.build 不是一个函数。 (在'this.build(json.message, page)'中,'this.build'是未定义的)

有人可以帮忙吗?顺便说一句,这段代码有哪些变化?

彼得

【问题讨论】:

    标签: javascript jquery ajax backbone.js


    【解决方案1】:

    您可能应该在调用 getJSON 之前保存当前上下文:

     ajax : function(parent_id,page) {
        var self = this;
        $.getJSON( "/**/**/"+parent_id+"/"+page, function( json ) {
            ...
            self.build(json.message, page);
    

    【讨论】:

    • 这很糟糕。您应该使用 jQuery ajax 的 context 选项。或者因为它是 2017 年 - 您可以根据上下文使用 bind 或箭头函数
    • 当然还有很多其他方法,但为什么是bad?并且.. 即使是 2017 年,这并不意味着每个人都使用现代网络浏览器,并且箭头功能并非无处不在。 bind 是很久以前的事了,但对我来说这让事情变得更加混乱
    猜你喜欢
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    相关资源
    最近更新 更多