使缓存工作需要哪些微风设置?

时间:2014-01-24 03:12:35

标签: javascript breeze

我认为Breeze会自动进行缓存,但在我下面的简单测试中它不起作用。为实现这一目标还需要哪些其他设置?

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 个答案:

答案 0 :(得分:2)

在文档中进一步阅读后,我看到没有关于对象的元数据,Breeze will do no caching

  

它们将是简单的JavaScript对象,而不是实体。 Breeze不会缓存它们,跟踪它们的变化,验证它们等等.Breeze只是作为一种HTTP检索机制而已,而不再是它。

以下是我的示例代码的更新(工作)版本:

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
});