Question

in silverlight and MVVM , I can fire the busy indicator for example

    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private bool _isBusy;
        public bool IsBusy 
        {
            get
            {
                return _isBusy;
            }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }


      ObervableCollection<OperationBase> _pendingOperations = new ObervableCollection<OperationBase>();
      public ViewModelBase()
      {

         _pendingOperations.CollectionChanged +=(s,e)=>
        {
            if( _pendingOperations.Count > 0)
               IsBusy = true // has operation going on
            else 
              IsBusy = false; //no operation

       }
  }

  void LoadData()
 {

    LoadOperation op = Context.Load(YourQuery, lo=>
    {
      _pendingOperations.Remove(lo); // lo is the operation, remove it 
      if (lo.HasError)
      {
            lo.MarkErrorAsHandled();
                MessageBox.Show(lo.Error.Message);
      }

    });

   _pendingOperations.Add(op);// add operation to the collection

 }

 void SaveData()
  {
       SubmitOperation so = this.context.SubmitChanges(s =>
       {

           _pendingOperations.Remove(s);                    

            if (s.HasError)
            {
                 s.MarkErrorAsHandled();
                 MessageBox.Show(s.Error.Message);
            }
        }, null);

       _pendingOperations.Add(so);// add operation to the collection }
    }

...

}

I want to do the same in MVC , any idea how to achieve that for example on search , create or any long process I need to show busy indicator and close it at the end , I know there's no property changed , am wonder if ther's any way

Was it helpful?

Solution

Assumeing you mean MVC -> ASP.NET MVC or something HTML/Javascript based with some sort of web server component.

In general you would have an animated gif (A spinning wheel for example) and show hide them while you are waiting for a long running operation.

In Javascript you can take advantage of Promises or use generic callback functions.

    //pseudo code
    loadData(function(data){
      // data came back async 
      // do something with data
     $('#loader').hide();
});

$('#loader').show();

or with promises

//method should return promise
var promise = loadData();
$('#loader').show();
promise.done(function(data){
  // do something with data
  $('#loader').hide();
});

you should of course do handle the error case too but same principles apply....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top