oData v4.0 - Enumerations Support
Hey,
I'm looking into replacing breeze with jaydata on my project, but i'm struggling with enumerations. I'm using the latest updates from nuget together with oData v4.0. I've generated my context with JaySvcUtil.exe without errors. Is the use of enumerations even possible? Can I define the type myself?
Exception: Unable to resolve type:Erp.Entity.Common.State
public class Category : BaseEntity
{
public int? ParentCategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string MetaTitle { get; set; }
public string MetaDescription { get; set; }
public int? PictureId { get; set; }
public bool LimitedToStores { get; set; }
public int DisplayOrder { get; set; }
public State State { get; set; } // enum
public bool SyncToShop { get; set; }
public bool Deleted { get; set; }
public DateTimeOffset? DateChanged { get; set; }
public DateTimeOffset? DateCreated { get; set; }
/// <summary>
/// Mapping
/// </summary>
public virtual Category ParentCategory { get; set; }
public virtual Picture Picture { get; set; }
public virtual ICollection<Category> SubCategories { get; set; }
}
public enum State
{
Normal = 0,
Changed = 1,
Night = 2
}
Generate by JaySvcUtil.exe:
$data.Entity.extend('Erp.Entity.Catalog.Category', {
'ParentCategoryId': { 'type':'Edm.Int32' },
'Name': { 'type':'Edm.String' },
'Description': { 'type':'Edm.String' },
'MetaTitle': { 'type':'Edm.String' },
'MetaDescription': { 'type':'Edm.String' },
'PictureId': { 'type':'Edm.Int32' },
'LimitedToStores': { 'type':'Edm.Boolean','nullable':false,'required':true },
'DisplayOrder': { 'type':'Edm.Int32','nullable':false,'required':true },
'State': { 'type':'Erp.Entity.Common.State','nullable':false,'required':true },
'SyncToShop': { 'type':'Edm.Boolean','nullable':false,'required':true },
'Deleted': { 'type':'Edm.Boolean','nullable':false,'required':true },
'DateChanged': { 'type':'Edm.DateTimeOffset' },
'DateCreated': { 'type':'Edm.DateTimeOffset' },
'Id': { 'key':true,'type':'Edm.Int32','nullable':false,'required':true },
'ParentCategory': { 'type':'Erp.Entity.Catalog.Category' },
'Picture': { 'type':'Erp.Entity.Media.Picture' },
'SubCategories': { 'type':'Array','elementType':'Erp.Entity.Catalog.Category' }
});
Are you going to support enumerated types in oData v4.0? I have faced the same problem using jayData v1.4.0 alfa.
Did you take a look at what the responses from your server look like? I have a Web Api OData v4 service, and enums are returned just like strings. So a response would look like: { 'Name': "my name', 'State': 'Changed' }
If that is the case, for now, just change the type of State in your JayData definition to Edm.String, and then recreate the enum on the javascript side with strings like this: var state = { Normal: "Normal", Changed: "Changed", Night: "Night" }
Depending on what kind of IDE you are using, you might even get around with just using plain strings, as I think recreating the enum only has advantages if your IDE has some kind of autocomplete for Javascript.
Yes, I did. Yes, response looked for enum exactly as you say. But, what about filtering over 'State' property? In case when we difine enum property as string then it should be escaped by ' symbol; and the simplest filter
.filter(function (it) { return it.State== this.state; }, { state: /**???**/'Changed'/**???**/ }) // may be wrong usage, but Edm model, defined by hand, suspect string usage.
Then we got
State eq 'Changed'
, while for enum we have to got (suppose enum name is 'StateEnum', and namespace is 'EnumNamespace'):
State eq EnumNamespace.StateEnum'Changed'
In case of enum isn't used as a collection of flags.
I tried to define enum by hand using followning code: my enum was:
namespace DAO.Model {
public enum EnumType {
Type1, Type2
}
}
The simplest definition for enum (may be wrong) would be:
$data.Class.define('DAO.Model.EnumType', null, null, {
constructor: function (name, value) {
console.log(name);
this.Name = name;
this.Value = value;
},
valueOf: function () {
return "DAO.Model.EnumType'" + this.Name + "'";
},
Name: { type: 'string' },
Value: { type: 'number'}
});
DAO.Model.EnumType.Type1 = new DAO.Model.EnumType("Type1", 0);
DAO.Model.EnumType.Type2 = new DAO.Model.EnumType("Type2", 1);
In this case:
.filter(function (it) { return it.Type== this.type; }, { state: DAO.Model.EnumType.Type1 })
the query would be created as expected (thanks to valueOf method).
BUT, how to register converter to get enum instance during deserialization collection in 'json' is parsed, I don't know. In this case for each entity instance would be called constructor EnumType without parameters. I't is important for me to get to know how to transform string to Enum, could you help me?
Ah, damn, I never used a filter on an enum, so I didn't knew my way of just using it as a string isn't working when filtering. Sorry, I don't have a solution for that right now. :(
Malyngo, It seems to me too that there is none simple solution for full enum support. But, are you going in the nearest future start to support enum type?
I am myself also just a user of Jaydata, and wanted to help the creator of the issue with this. Sorry if I have caused some confusion here.
They do not support oData4 just Odata2 which is why I could not use it. This is primarily based on their dependency on DATA.JS
http://datajs.codeplex.com/.
On Wed, Jun 24, 2015 at 5:05 AM, Rainer Ziller [email protected] wrote:
I am myself also just a user of Jaydata, and wanted to help the creator of the issue with this. Sorry if I have caused some confusion here.
— Reply to this email directly or view it on GitHub https://github.com/jaydata/jaydata/issues/195#issuecomment-114812669.
Ok, I just saw this. So now they do support 4. I wish they had this when I was trying to use it.
http://jaydata.org/blog/release-notes
On Wed, Jun 24, 2015 at 7:57 AM, Southpoint [email protected] wrote:
They do not support oData4 just Odata2 which is why I could not use it. This is primarily based on their dependency on DATA.JS
http://datajs.codeplex.com/.
On Wed, Jun 24, 2015 at 5:05 AM, Rainer Ziller [email protected] wrote:
I am myself also just a user of Jaydata, and wanted to help the creator of the issue with this. Sorry if I have caused some confusion here.
— Reply to this email directly or view it on GitHub https://github.com/jaydata/jaydata/issues/195#issuecomment-114812669.
+1 Need Flags to work (e.g. https://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c).
We are getting an error that a string can't be converted to the Enum type (because it is multiple Enum Flags together).
We are using OData v4.
Added New Issue specifically for this: https://github.com/jaystack/jaydata/issues/293