javascript - Backbone.js - Solving method -
backbone , jquery
i have piece of code:
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); } } });
in ajax method want build method
this.build(json.message, page);
but not work , error.
pm2.js:67typeerror: this.build not function. (in 'this.build(json.message, page)', 'this.build' undefined)
can help? changes in piece of code can way?
piotr
you should save current context before calling getjson:
ajax : function(parent_id,page) { var self = this; $.getjson( "/**/**/"+parent_id+"/"+page, function( json ) { ... self.build(json.message, page);
Comments
Post a Comment