3
我试图使用signalr和knockout.js显示数据库中的产品列表。但没有任何结果。任何人都可以告诉我我做错了什么或者我犯了什么错误?我将非常感谢帮助我。 这是视图:SignalR和Knockout viewmodel绑定
<div class="products">
<div class="row" data-bind="template: { name: 'productTemplate', foreach: products }">
</div>
<span class="messageClass" style="color: red;"></span>
</div>
<script type="text/html" id="productTemplate">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<div class="caption">
<h3 data-bind="text: name"></h3>
</div>
</div>
</div>
</script>
这是脚本:
<script>
$(function() {
function productViewModel(id, name) {
this.productId = id;
this.name = ko.observable(name);
var self = this;
}
function productListViewModel() {
this.hub = $.connection.voteHub;
this.products = ko.observableArray([]);
var products = this.products;
this.init = function() {
this.hub.server.getAllProducts();
}
this.hub.client.getAllProducts = function (allProducts) {
var mappedProducts = $.map(allProducts, function (item) {
return new productViewModel(item.productId, item.name)
});
products(mappedProducts);
}
}
var vm = new productListViewModel();
ko.applyBindings(vm);
$.connection.hub.start(function() {
vm.init();
});
});
</script>
这里是轮毂的方法来获取所有产品:如果我把我的枢纽代码像
public void GetAllProducts()
{
VoteViewModel viewModel = new VoteViewModel();
viewModel.Products = ProductService.GetProducts(new GetProductsRequest()).Products.ToList();
if (viewModel.Products != null)
{
// TODO: pomyslec nad wysylaniem listy a nie tablicy!
Clients.All.getAllProducts(viewModel.Products.ToArray());
}
}
这将工作得很好(取自演示程序):
VoteViewModel vm = new VoteViewModel();
vm.Products = new List<Product>() { new Product() { Name = "Sample", Id = 1 } };
Clients.All.getAllProducts(vm.Products.ToArray());
看起来像这样的代码虽然不起作用。 (我不知道为什么,也许是因为从DB我的产品对象,有更多的变量?):
VoteViewModel viewModel = new VoteViewModel();
viewModel.Products = ProductService.GetProducts(new GetProductsRequest()).Products.ToList();
Clients.All.getAllProducts(viewModel.Products.ToArray());
这里是我的产品类代码:(的DbContext类)
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
[Required]
public string ImagePath { get; set; }
public virtual ICollection<Vote> Votes { get; set; }
我做新ProductViewModel类看起来是这样的:
public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string ImagePath { get; set; }
public int VotesAmount { get; set; }
}
然后从数据库到模型绑定的所有数据,然后返回列表和现在的工作!但我真的不明白,为什么我不能只取产品列表从数据库中取出并发送给信号客户端(就像我之前试图做的那样)。
List<ProductViewModel> prods = new List<ProductViewModel>();
List<Product> products = ProductService.GetProducts(new GetProductsRequest()).Products.ToList();
foreach (var item in products)
{
ProductViewModel prod = new ProductViewModel()
{
Id = item.Id,
Name = item.Name,
Description = item.Description,
ImagePath = item.ImagePath,
VotesAmount = item.Votes.Count()
};
prods.Add(prod);
}
Clients.All.getAllProducts(prods.ToArray());
在我的javascript下我已经有了所有需要的引用,就像你写的这些。我修正了这个名字作为你的建议返回新的productViewModel到帕斯卡的情况,但即使现在当我调试代码获取到中心方法,它返回arrayList 2产品,但没有任何显示视图。我试图调试JavaScript,但它永远不会进入this.hub.client.getAllProducts来映射产品。我仍然没有这样的事情:(请帮助 – tzm
我已经在这里上传我的演示https://github.com/MisterFantastic/KnockoutSignalr请检查你的代码 – Thanigainathan
我编辑我的帖子上面,我开始改变捆绑, layout.cshtml,索引和它开始工作时,我试图改变一步一步的思考回到我的代码。它崩溃时,我改变代码在hub方法。我写了一切以上 – tzm