Base solution for your next web application
Open Closed

Get conts/enum in jquery ajax call #5679


User avatar
0
sbenfares created

Hi,

What is the best way to get Const/Enum value (declared in Core.Shared project) in abp/jquery ajax ?

I need to call an abp.service from js, and use a Const/Enum value in parameter

Thanks


7 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    hi,

    you can write the const/enum values in the page layout

     <script type="text/javascript">
            abp.enums.chatMessageReadState = {unread: @((int)ChatMessageReadState.Unread), read: @((int)ChatMessageReadState.Read)};
      </script>
    

    and in your JS file you can retrieve it:

    var unreadEnumValue = abp.enums.chatMessageReadState.unread
    

    I wrote the code on top my head. So it's just for guiding .

  • User Avatar
    0
    sbenfares created

    It's not possible

    My code is :

    
    <script type="text/javascript">
            abp.enums.documentsTypeCodes = {
                cvTypeCode: @((int)BlobConsts.Documents.Cv.TypeCode),
                pieceIdTypeCode: @((int)BlobConsts.Documents.PieceIdentite.TypeCode),
                diplomeTypeCode: @((int)BlobConsts.Documents.Diplome.TypeCode),
                autresDocsTypeCode: @((int)BlobConsts.Documents.AutresDocuments.TypeCode),
            };
        </script>
    

    Error is

    Uncaught TypeError: Cannot set property 'documentsTypeCodes' of undefined

  • User Avatar
    0
    alper created
    Support Team

    because abp.enums is undefined! try

    <script type="text/javascript">
            var abp = abp || {enums:{}}; 
            abp.enums.documentsTypeCodes = {
                cvTypeCode: @((int)BlobConsts.Documents.Cv.TypeCode),
                pieceIdTypeCode: @((int)BlobConsts.Documents.PieceIdentite.TypeCode),
                diplomeTypeCode: @((int)BlobConsts.Documents.Diplome.TypeCode),
                autresDocsTypeCode: @((int)BlobConsts.Documents.AutresDocuments.TypeCode),
            };
     </script>
    
  • User Avatar
    0
    sbenfares created

    Still not working.

    Cannot set property 'documentsTypeCodes' of undefined

    I found a workaround by using html attribute with this enum on my component ...

  • User Avatar
    0
    alper created
    Support Team

    sorry my bad!

    <script type="text/javascript">
      abp.enums = {
                documentsTypeCodes : {
                    cvTypeCode: @((int)BlobConsts.Documents.Cv.TypeCode),
                    pieceIdTypeCode: @((int)BlobConsts.Documents.PieceIdentite.TypeCode),
                    diplomeTypeCode: @((int)BlobConsts.Documents.Diplome.TypeCode),
                    autresDocsTypeCode: @((int)BlobConsts.Documents.AutresDocuments.TypeCode)
                }
            }    
     </script>
    
  • User Avatar
    0
    huntethan89 created

    Is there anyway we can generate JSON object automatically from Enums? So that we won't have to edit JS code everytime we have a new value in Enums.

  • User Avatar
    0
    alper created
    Support Team