2014-01-14 43 views
0

我使用signalr到prepand行到表,但我不能這樣做:前面加上一個行到表

HTML:

<div> 
    <table> 
     <thead> 
      <tr> 
       <td>Name</td> 
       <td>Price</td> 
      </tr> 
     </thead> 
     <tbody data-bind="foreach: Products"> 
      <tr> 
       <td><span data-bind="text: name"></span></td> 
       <td><span data-bind="text: price"></span></td> 
      </tr> 
     </tbody> 
    </table> 
</div> 

淘汰賽:

function ProductViewModel() { 
    Products = ko.observableArray(); 

    Products.unshift(
    { 
     name: Products.name, 
     price: Products.price 
    } 
); 
} 
ko.applyBindings(new ProductViewModel()); 

SignalR:

chat.client.hello = function (product) { 
     console.log(product.name); 
     ProductViewModel.Product(product); 
}; 
+0

什麼是不工作,併發生呢?你有任何錯誤? – nemesv

回答

0

它應該看起來像這:

function ProductViewModel() { 
    var self = this; 
    self.Products = ko.observableArray(); 
    self.AddProduct = function(product) { 
     self.Products.unshift(product); 
    }; 
} 

var productViewModel = new ProductViewModel(); 
ko.applyBindings(productViewModel); 

chat.client.hello = function(product) { 
    console.log(product.name); 
    productViewModel.AddProduct(product); 
};