0

我正在使用带有 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));
4

1 回答 1

0

试试这个代码。您还需要通过 addEntityType 方法调用将元数据添加到“altMs”。确保您也获得了最新版本的微风,v.0.78.x。不再需要代码中的“breeze.entityModel”。

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
});
于 2012-12-20T06:53:36.773 回答