3

I was looking at the demo: Telerik's Demo and this Telerik's Example

I still cannot figure out how, using this technique, to pass a parameter to the Read method, so instead of reading ALL games or products, it reads ONLY a subset of games or products, by category ID lest say. If I change the Read method in the server-side code to take a parameter, it never gets hit anymore, and I cannot figure out how to pass a parameter from the transport client-side definition definition... Any help would be highly appreciated!

  • Telerik's sample has an issue with update notifications. Data pushed from the server should follow the schema.data configuration. – TDenis Dec 18 '15 at 09:10

2 Answers2

0

I posted the question to the Telerik's forums and got this which should work! Telerik's Answer

0

In case anyone else comes along in the future and the link in the other answer is gone, or for those who don't want to download a project file and sift through it themselves, here's more detail from the answer given on the Telerik forums:

They use Kendo.DynamicLinq to "bind the request parameters."

They created a custom class that mirrors the structure of the existing DataSourceRequest class typically used in AJAX grid actions.

using System.Collections.Generic;
using Kendo.DynamicLinq;

namespace SignalRLocalHub.Models
{
    public class MyDataSourceRequest
    {
        public int Take { get; set; }
        public int Skip { get; set; }
        public int Page { get; set; }
        public int PageSize { get; set; }
        public Filter Filter { get; set; }
        public IEnumerable<Sort> Sort { get; set; }
        public IEnumerable<Aggregator> Aggregates { get; set; }
    }
}

In the ProductHub (SignalR hub) class, this is the Read method:

public DataSourceResult Read(MyDataSourceRequest request)
{
    return productService.Read()
        .ToDataSourceResult(
            request.Take, 
            request.Skip, 
            request.Sort, 
            request.Filter, 
            request.Aggregates);
}

The productService.Read method it calls, for reference, looks like this:

//int take, int skip, IEnumerable<Sort> sort, Filter filter, IEnumerable<Aggregator> aggregates
public IQueryable<ProductViewModel> Read()
{
    return entities.Products
        .OrderBy(p => p.ProductID)
        .Select(product => new ProductViewModel
        {
            ProductID    = product.ProductID,
            ProductName  = product.ProductName,
            UnitPrice    = product.UnitPrice.HasValue    ? product.UnitPrice.Value    : default(decimal),
            UnitsInStock = product.UnitsInStock.HasValue ? product.UnitsInStock.Value : default(short),
            Discontinued = product.Discontinued
        });
}

And finally, here is the grid's DataSource configuration:

.DataSource(d => d
    .SignalR()
    .AutoSync(true)
    .Transport(tr => tr
        .Promise("hubStart")
        .Hub("hub")
        .Client(c => c.Read("read").Create("create").Update("update").Destroy("destroy"))
        .Server(s => s.Read("read").Create("create").Update("update").Destroy("destroy"))
    )

    .ServerFiltering(true)
    .Filter(f => f.Add(m => m.UnitPrice).IsEqualTo(10))

    .ServerPaging(true)
    .PageSize(10)

    .Schema(s => s
        .Data("Data")
        .Total("Total")
        .Aggregates("Aggregates")
        .Model(m =>
        {
            m.Id(e => e.ProductID);
            m.Field(e => e.ProductID).Editable(false);
        })
    )
)

This allows sorting, paging, and filtering by any field that is part of the grid view model. So as long as your "category ID" is a property of the grid view model and the grid is configured to be able to filter by that field, this method should work.

CarenRose
  • 1,266
  • 1
  • 12
  • 24