问题
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