locked
JQuery syntax signalR Call RRS feed

  • Question

  • User-959394753 posted

    Hi,

     

    On an MVC application I want to lock records that are being edited. (see also my previous question )

    I realize now that I need to keep a list of Locked records on the server for this. I am doing this in memory since only a few users will be working with the application at the same time.

    So this is my Lock list:

    public class  LockList

        {

            static List<Lock> _LockList;

     

            static LockList()

            {

                _LockList = new List<Lock>();

            }

           

     

            public static void Add(long id, string psn)

            {

                Lock l = _LockList.SingleOrDefault(x => x.Id == id);

                if(l==null)

                {

                    Lock lo = new Lock();

                    lo.Id = id;

                    lo.Set = DateTime.Now.ToString();

                    lo.By = psn;

                    _LockList.Add(lo);

                }

               

            }

     

            public static void Remove(long id)

            {

                Lock l = _LockList.SingleOrDefault(x => x.Id == id);

                if (l != null) _LockList.Remove(l);

            }

     

            public static bool Contains(long id)

            {

                Lock l = _LockList.SingleOrDefault(x => x.Id == id);

                return l != null;

            }

     

            public static List<Lock> GetLockList()

            {

                return _LockList;

            }

     

     

            public class Lock

            {

                public long Id { get; set; } //The Id of the locked record

                public string  Set { get; set; } //The date and time of the lock

                public string By { get; set; } // the userId

            }

        }

     

    And this is my hub:

     

    public class HandOverHub : Hub

        {

            public void NotifyLock(long Id)

            {

                Debug.WriteLine("Locking " + Id);

                LockList.Add(Id,"");

                Clients.All.lockComplaint(Id.ToString());

            }

     

            public void NotifyUnLock(long Id)

            {

                Debug.Write("UnLocking " + Id);

                LockList.Remove(Id);

                Clients.All.unlockComplaint(Id.ToString());

            }

     

            public bool CheckLock(long Id)

            {

                return LockList.Contains(Id);

            }

        }

    Adding and removing these records is no problem. But when it comes to the Checklock function I am having some problems with the right jQuery syntax.

    What I am trying is to prevent opening an edit window when the signalR checklock returns true.

    First I tried this:

            if ($('SignalRFunctions').CheckLock(Id)) {  

                alert("This record is lock by another user.");

                return false;

            }

    But then I realized that signalR is working asynchronous so I did:

    $('SignalRFunctions').CheckLock(Id).done(function (value) {

                if (value) {

                    alert('This record is locked by another user.');

                    return false;

                }

                else {

                    mode = "edit";

                    CreateAddEditWindow();

                }

            });

    But I get the error message: SCRIPT5007: Unable to get property 'done' of undefined or null reference.

    So it looks like the syntax of the .done portion of the statement is not good. Can somebody help me how to do this??

    Also is this the right way to get a function value back from the server?

    Appreciate,

    Rob

    Wednesday, April 18, 2018 1:57 PM

Answers

  • User61956409 posted

    Hi Rob,

    If you put the following code in your edit button click function, does the code work as expected?

    var handoverhub = $.connection.handOverHub;
    
    handoverhub.server.checkLock(Id).done(function (val) {
    
        //your code logic
    
    })

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 22, 2018 8:05 AM

All replies

  • User753101303 posted

    Hi,

    Not sure to get your design even when  looking at the previous post. The UI element is not supposed to be hidden if locked and/or you don't keep a client side LockList which is updated using SignalR?

    For now it seems to me your intent is anyway to always make a server side call making maybe SignalR no more 100% needed ?

    You are 100% sure the much usual optimistic approach is not enough for you (maybe slightly adapted if needed).

    Wednesday, April 18, 2018 4:50 PM
  • User61956409 posted

    Hi Rob,

    Please refer to the following code to call your hub method from your JavaScript client:

    YourHubProxy.server.checkLock(Id).done(function (val) {
        //your code logic
    }).fail(function (error) {
        //your code logic
        }); 
    

    And I do a test with the similar code, which work for me.

    Hub method:

    public bool CheckLock(long Id)
    {
        bool istrue = false;
        if (Id>10)
        {
            istrue = true;
        }
        //return LockList.Contains(Id);
    
        return istrue;
    }
    

    Test result:

    With Regards,

    Fei Han

    Thursday, April 19, 2018 3:12 AM
  • User-959394753 posted

    Hi Fei,

    thanks for your answer.

    I realise that I did not inform you good regarding my Jquery code.

    First I have my document ready:


    $(document).ready(function () {

    SignalRFunctions();
    more code...
    })

    then to bundel all the signalR stuff:

    function SignalRFunctions() {
    var handoverhub = $.connection.handOverHub;

    //some more code

    // Start the connection with the servers Hub.
    $.connection.hub.start()
              .done(function () {
                    console.log('Now connected, connection ID=' + $.connection.hub.id);
                    $('#status').html("Connected to server!");

                   //Function to command the servers Hub to send a Lock Notification to all other users.(works ok)
                   $.fn.NotifyLockToAll = function (Id) {
                        handoverhub.server.notifyLock(Id);
                    };

                  //Function to command the servers Hub to send a UnLock Notification to all other users.(works ok)
                  $.fn.NotifyUnlockToAll = function (Id) {
                        handoverhub.server.notifyUnLock(Id);
                     };

                   //Function To check on the server if an item is locked. (Should return an bool )
                    $.fn.CheckLock = function (Id) {
                         handoverhub.server.checkLock(Id).done(function (val) {
                         return val;
                        });
              };

    //some more code
    });

    and then in an edit button click function I call:

    $('.EditBtn').click(function () {
             Id = ($(this).attr("data-Id"));

             var lock = $('SignalRFunctions').CheckLock();  //I think the problem is here but don't know what the correct syntax is.

             alert(lock);   //lock stays undefined!!

    }

    }

    Thursday, April 19, 2018 3:05 PM
  • User61956409 posted

    Hi Rob,

    If you put the following code in your edit button click function, does the code work as expected?

    var handoverhub = $.connection.handOverHub;
    
    handoverhub.server.checkLock(Id).done(function (val) {
    
        //your code logic
    
    })

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 22, 2018 8:05 AM
  • User-959394753 posted

    very stupid, but I did not realize I could declare te variable handoverhub more then once in my code.

    Monday, April 23, 2018 8:55 AM