У меня есть служба данных Breeze (он же datacontext) в моем одностраничном приложении. Я хочу получить список запусков от контроллера WebAPI вместе со списком OutlineItems для каждого запуска.
Контроллер возвращает список открытых запусков с дочерними элементами OutlineItem с помощью этого метода в BreezeController.
[AcceptVerbs("GET")]
public IQueryable<Run> Runs()
{
return _contextProvider.Context.Runs
.Include("RunOutlineItems")
.AsQueryable()
.Where(r => r.RunStatusId < 4); // 4 is the cutoff for open items
}
Вот модель данных.
namespace PilotPlantBreeze
{
public class Run
{
public int Id { get; set; }
public string Name { get; set; }
public string Rundate { get; set; }
public DateTime? RunStart { get; set; }
public int MachineId { get; set; }
public int ProductId { get; set; }
public int RunStatusId { get; set; }
public string Comments { get; set; }
// Nav props
public IList<OutlineItem> RunOutlineItems { get; set; }
}
}
Когда я смотрю на данные ответа из запроса WebAPI, список RunOutlineItems находится в JSON. Вот, например, один предмет:
{
"$id":"27",
"$type":"PilotPlantBreeze.OutlineItem,PilotPlantBreeze.Model",
"Id":22,
"RunId":5,
"TankId":4,
"OutlineTopicId":1,
"OutlineDescriptionId":9,
"PersonId":1,
"Value":"23"
}
Вот мой код javascript на стороне клиента для получения данных из WebAPI. Проверка ошибок и проверка локального кеша опущены для ясности.
var getRuns = function () {
// The EntityQuery is defined at the beginning of the dataservice
// Here I am asking for a query on the server. Note the .expand
// which is supposed to avoid lazy loading. Lazy loading is turned off
// on the WebAPI already.
var query = EntityQuery
.from("Runs")
.expand("RunOutlineItems")
.orderBy("rundate");
// The manager is defined at the beginning of the dataservice
// Here I am asking the manager to execute the query with a promise
return manager.executeQuery(query)
.then(runsQuerySucceeded)
.fail(runsQueryFailed);
// The promise does not fail, but I would put an error in here if it ever does
function runsQueryFailed(data) {
}
// When the promise succeeds, the data parameter is the JSON from the WebAPI
function runsQuerySucceeded(data) {
//
// When I stop the debugger here data.results holds the entities, but
// the child entities for the RunOutlineItems is an observableArray with
// nothing in it.
//
app.vm.runs.runs(data.results);
}
};
Итак, мой вопрос заключается в том, как получить дочерние элементы в моей модели просмотра. У меня есть обходной путь, который получает дочерние элементы в отдельном вызове WebAPI на сервере и обрабатывает их с помощью пользовательского ko.bindHandler, но было бы удобно, чтобы работала технология навигации.