我正在使用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));
答案 0 :(得分:0)
试试这段代码。您还需要将元数据添加到&#39; altMs&#39;通过addEntityType方法调用。请确保您获得最新版本的breeze,v.0.78.x。 &#39; breeze.entityModel&#39;在您的代码中不再需要。
var DataService = breeze.DataService;
var MetadataStore = breeze.MetadataStore;
var EntityManager = breeze.EntityManager;
var dataService = new DataService({
serviceName: altServiceName,
hasServerMetadata: false
});
var altMs = new MetadataStore({
namingConvention: NamingConvention.camelCase
});
return new EntityManager({
dataService: dataService,
metadataStore: altMs
});