What breeze settings are needed to make caching work?

非 Y 不嫁゛ 提交于 2019-12-11 05:40:50

问题


I thought that Breeze does caching automatically, but in my simple test below it does not work. What other settings are needed to make this happen?

var dataService = new breeze.DataService({
    serviceName: 'api',
    hasServerMetadata: false
});

var manager = new breeze.EntityManager({ dataService: dataService });
var metadataStore = manager.metadataStore;

console.log('before fetch', manager.getEntities());// returns [] as expected

var query = breeze.EntityQuery.from("ContentTypes");
manager.executeQuery(query).then(function(data) {
    console.log(data.results.length); // 3
    console.log('after fetch', manager.getEntities()); // still []. why???
});

回答1:


After reading further in the documentation I see that without metadata about the objects, Breeze will do no caching:

They will be simple JavaScript objects, not entities. Breeze won't cache them, track their changes, validate them, etc. Breeze is acting only as an HTTP retrieval mechanism and no more.

Below is an updated (working) version of my sample code:

var dataService = new breeze.DataService({
    serviceName: 'api',
    hasServerMetadata: false
});

var manager = new breeze.EntityManager({ dataService: dataService });
manager.metadataStore.addEntityType({
    shortName: "ContentType",
    namespace: "MyCompany.MyProduct.Models",
    autoGeneratedKeyType: breeze.AutoGeneratedKeyType.None,
    dataProperties: {
        Name: { dataType: breeze.DataType.String, maxLength: 30, isNullable: false, isPartOfKey: true },
        Description: { dataType: breeze.DataType.String, maxLength: 60, isNullable: false }
    }
});

//
// these lines are the same as before, but now they work because of the metadata
//
console.log('before fetch', manager.getEntities()); // returns [] as expected

var query = breeze.EntityQuery.from("ContentTypes");
manager.executeQuery(query).then(function(data) {
    console.log(data.results.length); // 3
    manager.addEntity(data.results[0]);
    console.log('after fetch', manager.getEntities()); // now it returns 3 entities
});


来源:https://stackoverflow.com/questions/21323736/what-breeze-settings-are-needed-to-make-caching-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!