0

I wrote two functions,assume that one of them get us list of all notifications and one of them get new notification, i wrote a script which call first method and get list of all notifications in Json format, and write another script which call second method every 8 second, and get us new notifications in Json format too. i show these notifacations in a KendoUI Datasource. so i have two datasource for just one KendoUI Datasource component, i want to add two data source in one datasource, is there any way to do this?

EDIT: This is my code

 <script id="template" type="text/x-kendo-template">
             
          <tr>

            <td>#= ID #</td>
            <td>#= TITLE #</td>
            <td>#= DESC #</td>
               
           </tr>
  </script>

My first script which get us list of all notification :

       var datas = function () {

                        var objects = [];
                        $.ajax({
                            type: "POST",
                            url: "./WebForm1.aspx/GetNoti",
                            data: {},
                            async: false,
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success:
                                function (response) {

                                    for (var i = 0; i < response.d.length; i++) {

                                        objects.push({ 'ID': response.d[i].ID, 'TITLE': response.d[i].TITLE, 'DESC': response.d[i].DESC });

                                    }
                                },

                        });
                        return objects;
                    };




                    var dataSource = new kendo.data.DataSource({
                        data: datas(),
                        change: function () {
                            $("#movies tbody").html(kendo.render(template, this.view()));
                        }
                    });

                   dataSource.read();

and this is my seccond script which call method that give us new notifications every 8 sec:

     $("#go").click(function () {
                        setInterval(
                            function () { test2();}, 8000);
                    });

        var p = function () {
                        var objects = [];
                        $.ajax({
                            type: "POST",
                            url: "./WebForm1.aspx/GetUnCheckNotification",
                            data: {},
                            async: false,
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success:
                                function (response) {

                                    for (var i = 0; i < response.d.length; i++) {

                                        objects.push({ 'ID': response.d[i].ID, 'TITLE': response.d[i].TITLE, 'DESC': response.d[i].DESC });

                                    }
                                },

                        });
                        return objects;

                    };

          function test2() {

                        var dataSource2 = new kendo.data.DataSource({
                            data: p(),
                            change: function () {
                                $("#movies tbody").html(kendo.render(template, this.view()));
                            }

                        });
                        dataSource2.read();

                    }

Now i want some thing like this :

dataSource = dataSource + dataSource2

dataSource.read();

Is there anyway?

Community
  • 1
  • 1
pmn
  • 2,176
  • 6
  • 29
  • 56

2 Answers2

1

JSON format cab be a nested structure. Define a view model and then use it.

public class Report
{
    public int Id {set; get;}
    public string Title {set; get;}
    public string Desc {set; get;}
}

public class MyReportViewModel
{
  public List<Report> NewNotifications {set;get;}
  public List<Report> AllNotifications {set;get;}
}
  • Then serialize this new MyReportViewModel { ... } using Json.NET library (server side).
  • On the client side, you can use the returned JSON format as usual.
Community
  • 1
  • 1
VahidN
  • 18,457
  • 8
  • 73
  • 117
  • What you're mean is i dont need two script one for AllNotifications and another for NewNotifications ? what about my method ? can you explain me more about this please? – pmn Feb 19 '14 at 08:54
  • Yes, you can combine them together as a view model + To signal users about the new changes from server, it's better to use `SignalR` and not just querying the server every 8 seconds. SignalR is much more efficient for these kinds for tasks. – VahidN Feb 19 '14 at 09:01
  • Thanks,,i confuse abit:( about my method i write two method with StoredProcedure one of them give use All notification and the other one is new notification with where condition uncheck=true, i write webmethod method, i should make change on these methods and SP or not?? – pmn Feb 19 '14 at 09:15
  • no. you have 2 properties here in `MyReportViewModel`. create a new instance of it and return it as a json. also you can apply `caching` for the all rows parts as well (server side). – VahidN Feb 19 '14 at 10:31
  • I should read notification from my DataBase! how can i can do it?? sorry, I really confused – pmn Feb 19 '14 at 10:35
  • read them from the database (as before) and then assign these values to the properties of the view model. you wanted a single data source for your client side grid. this view model gives you that. it's a single nested object. – VahidN Feb 19 '14 at 12:36
  • In this way if i wrote a script that call the method ( which Serialize the 'new MyReportViewModel { ... }') every 8sec, it's call two method which assign to two properties every 8sec, so it gives us both of AllNotification and NewNotification , but i want a way which the first time give us AllNotification(so just once time call the method which assign to AllNotifications property) and every 8 sec give us NewNotification(so it call just the method which assign to NewNotifications property every 8sec).. are u understand what i mean? – pmn Feb 19 '14 at 21:53
  • Try caching (Caching API, Using the Cache Object) http://msdn.microsoft.com/en-us/library/aa478965.aspx – VahidN Feb 19 '14 at 22:17
0

Never tried anything like this. But when I was looking for your answer, I found this link at Telerik Forums.

Two Data Sources, One Grid

This might help.! :)

Vivek Parekh
  • 1,075
  • 9
  • 25