我正在使用带有 Asp.Net WebAPI 的 Breeze.js 运行以下命令。我可以使用 OData 协议查询我的服务,并且可以在响应中看到 JSON 数据,但是 .then() 和 .fail() 回调没有触发,因此视图模型永远不会获取数据。那是因为我缺少元数据吗?
/// <reference path="..\breeze.debug.js" />
(function (root) {
var dataService = new breeze.DataService({
serviceName: 'api/breezesample',
hasServerMetadata: false
});
var altMs = new breeze.MetadataStore({
namingConvention: breeze.NamingConvention.camelCase
});
var manager = new breeze.EntityManager({
dataService: dataService,
metadataStore: altMs
});
// define the viewmodel
var vm = {
todos: ko.observableArray(),
includeDone: ko.observable(false),
show: ko.observable(false)
};
// start fetching Todos
getTodos();
// re-query when "includeDone" checkbox changes
//vm.includeDone.subscribe(getTodos);
// bind view to the viewmodel
ko.applyBindings(vm);
/* Private functions */
// get Todos asynchronously
// returning a promise to wait for
function getTodos() {
var query = breeze.entityModel.EntityQuery.from("todos");
if (!vm.includeDone()) {
query = query.where("IsDone", "==", false);
}
return manager
.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
};
function querySucceeded(data) {
vm.todos.removeAll();
var todos = data.results;
todos.forEach(function (todo) {
vm.todos.push(todo);
});
vm.show(true); // show the view
}
function queryFailed(error) {
alert("Query failed: " + error.message);
}
}(window));