我正在尝试实现 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;
}
}
我的 View 模型
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 is not a function at viewModel.self.init (About:92) at hubConnection.fn.init. (About:168) at hubConnection.fn.init. (jquery.signalR-2.2.0.js:546) at hubConnection.fn.init.dispatch (jquery.js:4732) at hubConnection.fn.init.elemData.handle (jquery.js:4544) at Object.trigger (jquery.js:7788) at jQuery.fn.init.triggerHandler (jquery.js:7878) at jquery.signalR-2.2.0.js:614 at jquery.signalR-2.2.0.js:1114 at Object.success (jquery.signalR-2.2.0.js:1450)
最佳答案
我相信集线器方法区分大小写。您应该将 javascript 更改为:
self.hub.getAll();
Dennis1679 在下面的评论中强调了一种检查所需案例的有用方法。
关于javascript - knockout.js 信号 r getall 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43737095/