我正在尝试实施this示例
<script src="/signalr/hubs"></script>
<div id="div1">
<textarea data-bind="value: commenttext"></textarea><br />
<a href="#" data-bind="click: addcomments" style="margin-bottom:4em">Send</a><br /><br /><br />
<ul data-bind="foreach: comments,visible: comments().length > 0">
<li>
<span data-bind="text:commentdate"></span>
<strong><span data-bind="text: commentsname"> </span></strong><br /><br />
</li>
</ul>
</div>
我的chatHub类
public class chathub : Hub
{
public void Send(string name, string message)
{
Clients.All.broadcastMessage(name, message);
}
public void GetAll()
{
List<CommentsViewModel> boards = new List<CommentsViewModel>();
CommentsViewModel b = new CommentsViewModel();
b.commentid = 1;
b.commentsname = "a";
b.commentdate =System.DateTime.Now;
b.cardid = 1;
boards.Add(b);
CommentsViewModel bb = new CommentsViewModel();
bb.commentid = 2;
bb.commentsname = "b";
bb.commentdate = System.DateTime.Now;
bb.cardid = 1;
boards.Add(bb);
Clients.All.taskAll(boards);
}
public bool Add(Comment newComment)
{
Comment commentobj = new Comment();
commentobj.comment = newComment.comment;
commentobj.commentdate = System.DateTime.Now;
commentobj.cardid = newComment.cardid;
Clients.All.addComm(commentobj);
return true;
}
}
我的viewmodel
var viewModel = function () {
var self = this;
this.hub = $.connection.chathub;
self.commenttext = ko.observable();
self.comments = ko.observableArray();
self.commentdate = ko.observable();
self.commentsname = ko.observable();
self.init = function () {
self.hub.getall();
}
self.hub.addComm = function (c) {
self.comments.unshift({
commentid: c.commentid,
commentsname: c.comment,
cardid: c.cardid,
commentdate: c.commentdate
});
};
self.hub.taskAll = function (allcomments) {
$.getJSON("@Url.Action("GetComments", "Home")?id=" + self.selectedcard(), function (data) {
self.comments(data);
})
}
self.addcomments = function () {
var t = { "comment": self.commenttext(), "cardid": self.selectedcard() };
$.ajax({
url: "@Url.Action("AddComments", "Home")",
type: "post",
contentType: "application/json",
data: JSON.stringify(t) ,
success: function (data)
{
self.hub.add(t).done(function () {
console.log('Success!')
// }).fail(function (e) {
/ console.warn(e);
});
//self.comments.unshift({
// commentid: data.commentid,
// commentsname: data.comment,
// cardid: data.cardid,
// commentdate: data.commentdate
//});
// self.commenttext("");
}
});
};
};
var vm = new viewModel();
ko.applyBindings(vm);
$.connection.hub.start(function () { vm.init(); });
为什么我收到此错误?
ncaught TypeError:self.hub.getall不是函数 at viewModel.self.init(约:92) 在hubConnection.fn.init。 (关于:168) 在hubConnection.fn.init。 (jquery.signalR-2.2.0.js:546) 在hubConnection.fn.init.dispatch(jquery.js:4732) at hubConnection.fn.init.elemData.handle(jquery.js:4544) at Object.trigger(jquery.js:7788) 在jQuery.fn.init.triggerHandler(jquery.js:7878) 在jquery.signalR-2.2.0.js:614 在jquery.signalR-2.2.0.js:1114 在Object.success(jquery.signalR-2.2.0.js:1450)
答案 0 :(得分:2)
我认为hub方法区分大小写。您应该将您的javascript更改为:
self.hub.getAll();
Dennis1679强调了在下面的评论中检查所需案例的有用方法。