我有一个mainoops.js文件,其中包含以下代码
var Notifier = function(){
this.socket = io.connect('http://localhost:8080');
this.currentdate = new Date();
}
Notifier.prototype.addCbNotification = function(message){
var datetime = this.currentdate.getDate() + "/" + (this.currentdate.getMonth()+1) + "/" + this.currentdate.getFullYear() + " @ " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();
var datamessage = " <div class='row'><div class='col-xs-2'><i class='fa fa-envelope'></i></div><div class='col-xs-10'><h5><a href=''>" + message + "</a></h5><small>"+datetime+"</small></div></div>";
$("#callback-btn").addClass('alert-notice');
$( "#callback" ).append( "<li class='list-group-item unread'>"+datamessage+" </li> " );
}
Notifier.prototype.addNotification = function(message){
var datetime = currentdate.getDate() + "/" + (currentdate.getMonth()+1) + "/" + currentdate.getFullYear() + " @ " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();
var datamessage = " <div class='row'><div class='col-xs-2'><i class='fa fa-envelope'></i></div><div class='col-xs-10'><h5><a href=''>" + message + "</a></h5><small>"+datetime+"</small></div></div>";
$("#notice-btn").addClass('alert-notice');
$( "#notice" ).append( "<li class='list-group-item unread'>"+datamessage+" </li> " );
}
Notifier.prototype.startsocketforcallbback = function(myid) {
// body...
socket.on('callback', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addCbNotification(message[1]);
}
});
};
Notifier.prototype.startsocketfornotice = function() {
// body...
socket.on('notice', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addNotification(message[1]);
}
});
};
我在我的php文件中调用它,如下所示
<script src="{{asset('/js/mainoops.js')}}"></script>
但是当我尝试在PHP页面中实例化它时,就像这样
<script>
var obj = new Notifier();
obj.startsocketforcallbback('<?php echo Auth::user()->empid; ?>');
obj.startsocketfornotice();
</script>
我正在关注错误
Uncaught ReferenceError: Notifier is not defined
答案 0 :(得分:0)
不幸的是,this
是您的回调没有指向您的通知程序实例。尝试使用on(..)
的第3个参数:
socket.on('callback', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addCbNotification(message[1]);
}
},this);
<{1>}和中的
Notifier.prototype.startsocketforcallbback
socket.on('notice', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addNotification(message[1]);
}
},this);
中的
答案 1 :(得分:0)
使用jquery:
jQuery(document).ready(function($) {
var obj = new Notifier();
obj.startsocketforcallbback('<?php echo Auth::user()->empid; ?>');
obj.startsocketfornotice();
});