问题
I setup WebApi method for Lookups which return
public Object Lookups()
{
var durations = Enum.GetNames(typeof(Duration));
var status = Enum.GetNames(typeof(Status));
return new { durations, status };
}
Then I make a query to the server with breeze
return entityQuery.from('Lookups')
.using(manager).execute()
.then(querySucceeded, _queryFailed);
function querySucceeded(data) {
console.log("Retrieving [Lookups] " + data);
return true;
}
Now later on I want to get those lookups from cache, please note that in metadata my Enums reside under "EnumType", It looks like breeze not provide support yet to retrieve enums from server side so I returning them as a Lookups. Now, I am wondering how can I get them locally to reduce extra round trip?
Thanks
回答1:
.NET Enums are supported with the regular Web API. Their type names appear in metadata and are recognized by Breeze.
I don't think the enum values are propagated to the client automatically (that would be nice) but you can send them to the client by other means such as you are doing with a lookups endpoint.
The Web API OData does not support enums yet either. In fact, it dies if a property returns an enum whereas WCF Data Services is content to deal with their string names. Nothing we can do about this defect of Web API OData at the moment.
Enums are not entities and I don't think you want to fake them as entities on the client. You could but I wouldn't.
Because they are not entities, Breeze won't store them in cache. Your query returns JavaScript simple objects, not entities, and there is no cache query that could find them.
I think what I would do in your situation is capture these enum values as array properties of a datacontext
service ... the same service that your are using (I hope) to encapsulate your Breeze-based data access activities so that ViewModels interact with the datacontext
rather than directly with Breeze components.
Accordingly, your lookups query success callback might look like this:
// Inside the datacontext service
var isReady = null;
var dc = {
ready: ready,
Duration: [],
Status: [],
...
}
return dc;
///////////////
function ready() {
return isReady || (isReady = getReady())); // returns the ready promise
}
function getReady() {
// stuff you do to prime the pump
// returns a promise that is "true" when you've completed your
// preparatory async tasks such as fetching lookups
...
}
...
function lookupsQuerySucceeded(data) {
console.log("Retrieved [Lookups] " + data);
dc.Duration = data.results.Duration;
dc.Status = data.results.Status;
return true;
}
I'm leaving out details but I hope you get the idea.
There is only one trip to the server with this technique, a single visit to get the initial stuff and populate the cache and/or (in this case) properties of the datacontext
itself.
回答2:
If your meta data (I have loaded into a variable window.app.metadata) contains enum values, then you can
1. Write js code to extract those enum in js dictionary
JSON.parse(window.app.metadata).schema.enumType.forEach(function (enumType) {
var newEnumValues = [];
enumType.member.forEach(function (enumValue) {
var newEnumValue = { id: enumValue.value, name: enumValue.name };
newEnumValues.push(newEnumValue);
});
enumDictionary[enumType.name] = newEnumValues;
});
2. Write a method to get enum value based upon enum name and id.
function GetEnumDictionaryValue(enumName, enumValueId) {
var result = null;
enumDictionary[enumName].some(function (enumValue) {
if (enumValue.id == enumValueId) {
result = enumValue.name;
return;
}
});
return result;
}
来源:https://stackoverflow.com/questions/23979265/breeze-local-query-for-enumtype