Hello Support,
We're using DataSource in a stand-alone manner, simply using it as a gateway to and from Web API endpoints. It's been great for reads. But now we've come to editing and creating, and we're not having any luck.
Consider the projectsDataService below. The projectsDataService provides a gateway to an enumerable of Project, which has all of the familiar fields you would expect of a Project class:
All of the computed-s on the model are available to Knockout, as expected. Nothing wrong there. But I have a couple of questions:
We're using DataSource in a stand-alone manner, simply using it as a gateway to and from Web API endpoints. It's been great for reads. But now we've come to editing and creating, and we're not having any luck.
Consider the projectsDataService below. The projectsDataService provides a gateway to an enumerable of Project, which has all of the familiar fields you would expect of a Project class:
01.
define('projectsDataService', ['underscore'],
02.
function(_) {
03.
var projectsDataSource = new kendo.data.DataSource({
04.
transport: {
05.
read: {
06.
url: "api/projects",
07.
dataType: "json"
08.
},
09.
update: {
10.
url: "api/projects",
11.
dataType: "json",
12.
type: 'PUT'
13.
}
14.
},
15.
page: 1,
16.
pageSize: 20,
17.
schema: {
18.
model: {
19.
location: function() {
20.
return this.projectAddress.city + ", " + this.projectAddress.state.abbr;
21.
},
22.
encodedProjectAddress: function () {
23.
//implement Strategy pattern for encoding
24.
var fullAddress = this.projectAddress.streetAddress + ",+" +
25.
this.projectAddress.city + ",+" +
26.
this.projectAddress.state.abbr;
27.
28.
return fullAddress.replace(/ /g, "+");
29.
},
30.
multilineProjectAddress: function() {
31.
var address = [this.projectAddress.streetAddress,
32.
'<
br
/>',
33.
this.location() + ' ' + this.projectAddress.postalCode].join('\n');
34.
35.
return address;
36.
},
37.
created: function() {
38.
return moment(new Date(this.createdAt)).fromNow();
39.
}
40.
}
41.
}
42.
}),
43.
44.
getProjects = function (projectsObservableArray) {
45.
projectsDataSource.fetch(function() {
46.
var dataView = projectsDataSource.view();
47.
var projects = [];
48.
_.each(dataView, function (item) {
49.
50.
projects.push(ko.observable(item));
51.
});
52.
projectsObservableArray(projects);
53.
});
54.
},
55.
56.
saveProjects = function() {
57.
projectsDataSource.sync();
58.
};
59.
60.
return {
61.
getProjects: getProjects,
62.
saveProjects: saveProjects
63.
};
64.
}
65.
);
- ProjectAddress is a nested complex type, so that I have projectAddress.streetAddress, projectAddress.city, projectAddress.state, etc. Changes in these properties are, in fact, manifested in the UI (courtesy of Knockout), but changing them doesn't trigger a call to the transport. In other words, the changes never make it to the server. I get the sense that DataSource is expecting flattened data. If so, how do I go about doing that the "DataSource" way?
- Is the fields property necessary on the model, and what is the difference between binding to a field on the model, and binding to a field in the fields property?
- Consider the getProjects() function. I'm stuffing a knockout array with an array of oversables--the projects. Should I be making each of Project's properties observable as well in order for the DataSource to pick up on the change? The problem is that I'm seeing changes in the client, even across multiple screens, ut no call to the server.
- Is the "id" property, or field, necessary on the model?
- Is it necessary to mapevery property ("field") on the model, or just those that are interesting, as in the code above?
Thank you.
Eric
Eric