2014-05-02 26 views
0

Silverlight的BusyIndi​​cator控件,我可以解僱例如繁忙的指標相當於Silverlight和MVVM在MVC

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 } 
    } 

... 

} 

我想要做同樣的MVC,任何想法如何做到這一點,例如搜索,創建或者任何長期的過程,我需要表現出繁忙的指標,並在年底關閉它,我知道有沒有改變性質,我不知道是否療法的任何方式

+2

在ASP.NET MVC?然後你需要在Javascript中完成它。 –

+0

任何想法如何使其通用像繁忙指標 – AMH

回答

1

Assumeing你的意思是MVC - 基於> ASP.NET MVC什麼HTML/JavaScript的與某種Web服務器組件。

一般來說,你會得到一個動畫gif(例如一個旋轉輪),並在你等待長時間運行的操作時顯示隱藏它們。

在Javascript中,您可以利用Promises或使用通用回調函數。

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

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

或承諾

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

你當然應該做太多處理錯誤的案件,但同樣的原則也適用....

+0

對不起,但我沒有得到它我的邏輯是在控制器類 – AMH

+1

這是在客戶端與JavaScript(jQuery)。當你通過AJAX請求控制器您期望在顯示加載indecator的同時進行部分更新。 (就像在Silverlight中它發生在客戶端而不是服務器上) – silverfighter