This is a migrated thread and some comments may be shown as answers.

Using data to send additional data EVERY read()

1 Answer 399 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Devon
Top achievements
Rank 1
Devon asked on 04 Jun 2013, 08:11 AM
Hello

I am running into a problem  using the data field on the transport read. The additionalData function is called  only the 1st time the datasource executes read. 
I have an event that updates the viewModel and the calls read() on the datasource. I can confirm that  it makes a call to the server every time but it only calls additionalData the 1st time.
Here are extracts of the related bits of code.

//in the datasource setup
transport: {
    read: {
    url: crudServiceBaseUrl + "/NoteSource",
    dataType: "json",
     data: App.Notes.additionalData(),
    cache: false
  },
  create: {
    url: crudServiceBaseUrl + "/Editing_Create",
    dataType: "json"
   }
 }

//in App.Notes 
where additionalData is:
additionalData: function () {
    console.log("additionalData");
    console.log(App.Notes.viewModel.linkedModel());
    return { entity: App.Notes.viewModel.linkedModel(), entityId: App.Notes.viewModel.linkedModelId() };
}
in the App.Notes namespace.

and viewModel is:
viewModel: {
                linkedModel: ko.observable(""),
                linkedModelId: ko.observable(0),
                username: ko.observable(""),
                noteType: ko.observable(""),
                createdAt: ko.observable(new Date())
            }
also in the same namespace.

The event handler contains the following that triggers the read and subsequent call to the server:
listView.dataSource.read();

Also just as a side note. This worked when i used the mvc version but I then got a stack overflow when I tried to include a partial of the cshtml page that contains this in my _Layout.
@(Html.Kendo().ListView<TimeTarget.RDM.Core.DataTransferObjects.NoteDto>()
                .Name("listView")
                .TagName("div")
                .ClientTemplateId("template")
                .Editable()
                .DataSource(dataSource => dataSource
                    .Model(model => model.Id("NoteId"))
                    .Create(create => create.Action("Editing_Create", "Note"))
                    .Read(read => read.Action("NoteSource", "Note")
                    .Data("App.Notes.additionalData")
                    ) // Specify the action method and controller name

                )
                .Pageable(paging => paging.Enabled(true))
                  )

1 Answer, 1 is accepted

Sort by
0
Accepted
Nikolay Rusev
Telerik team
answered on 05 Jun 2013, 06:01 AM
Hello Devon,

There is difference in how you set the data field of the read action:
 -  data: App.Notes.additionalData()
   will execute the function immediately, it will return an object and that object will be used. It is like the following code snippet (try it in the browser console):

function foo() {
 console.log("executing  foo");
}
 
console.log("start init of bar");
var bar = foo(); // this will execute the `foo` function immediately
console.log("end init of bar");


 - data: App.Notes.additionalData
   in this case the data is set to a function, this function must be called for every request in order to retrieve data structure that can be serialized and send to server. It works as the following snippet
function foo() {
 console.log("executing  foo");
}
 
console.log("start init of bar");
var bar = foo; //this will *not* execute the function
console.log("end init of bar");
bar(); //this will execute the function

Here are two examples showing the difference:
 - data as a function : http://jsbin.com/imuxov/2/edit
 - data as a object : http://jsbin.com/apotol/1/edit

Regards,
Nikolay Rusev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Data Source
Asked by
Devon
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Share this question
or