2017-10-06 71 views
2

我正在使用很多Kendo UI窗口。有什麼方法可以在全球範圍內以某種方式指定默認值?或者,也許更現實的版本,我可以創建一些父母預定義的值,然後只是覆蓋我需要改變的值?全局設置Kendo UI窗口值

例如,我想同樣的錯誤行爲,併爲所有窗口的模態參數,所以我想這樣做:

$("#parentWindow").kendoWindow({ 
    modal: true, 
    error: function() { 
      this.close(); 
      new Notification().error(); 
    } 
}); 

然後使用父窗口爲新基地窗戶:

$("#newWindow").kendoWindow({ 
    title: "This window should have the options (modal and error) of the parentWindow",  
}).??getTheRestOfTheValuesFromParent()??; 

或重寫一些參數:

$("#newWindow2").kendoWindow({ 
    modal: false, 
    title: "A window with overwritten modal parameter",  
}).??getTheRestOfTheValuesFromParent()??; 

是它在某種程度上可能爲了達到這個目的,有沒有可能像C#繼承一樣? 也許這是一個愚蠢的問題,但我不熟悉JS。

回答

5

我強烈建議您創建自己的包裝代碼覆蓋所有 - 或者至少是那些更復雜的 - 劍道小部件。我的團隊多年來一直在使用kendo進行一切項目,我們的結果非常積極。主要目的是你需要:全球行爲。如果在您的項目上編碼了千個窗口之後,您需要將它們全部更改,只需更改包裝器即可。這只是一個簡單的jQuery功能:

$.fn.MyWindow = function(options) { 
    var $target = $(this); 
    var widget = { 
     _defaultOptions: { 
      actions: ["Minimize", "Maximize", "Close"], 
      visible: false, 
      width: 400, 
      height: 400, 
      modal: true 
     }, 
     _options: options || {}, 
     _target: $target, 
     _widget: null, 

     _init: function() { 
      this._manageOptions(); 
      this._createWidget(); 

      return this; 
     }, 

     _manageOptions: function() { 
      // Here you can perform some validations like displaying an error when a parameter is missing or whatever 
      this._options = $.extend(this._options, this._defaultOptions); 
     }, 

     _createWidget: function() { 
      this._widget = this._target.kendoWindow(this._options).data("kendoWindow"); 

      // Create here some behaviours that the widget doesn't haves, like closing the window when user click the black overlay 
      if (this._options.closeOnOverlayClick) { 
       $('body').off('click', '.k-overlay').on('click', '.k-overlay', function() { 
        this._widget.close(); 
       }.bind(this)); 
      } 
     }, 

     Show: function(center) { 
      if (center) { 
       this._widget.center(); 
      } 

      this._widget.open(); 
     } 
    }; 

    return widget._init(); 
}; 

var wnd = $("#wnd").MyWindow({ 
    title: "My first window", 
    closeOnOverlayClick: true // Your own parameter 
}); 

// Now you work with your own functions: 
wnd.Show(true); 

Demo

有這麼多的自定義,你自己喜歡的活動 - 其中一些劍道的部件並不富人 - 等。

+1

謝謝你,它看起來非常有用 –

3

我只想補充一點,有一篇文章(here)有關創建自定義劍道部件,其中您可以找到更多關於可能實施的不同場景的具體信息。

+1

我知道有一些官方包裝一個小部件,但我不記得在哪裏可以找到它。好文章。 – DontVoteMeDown

+0

絕對有用,非常感謝,但由於有特定的代碼,我寧願接受第一個答案。 –

3

Ι有一個像你的情況與劍道窗口,劍道網格和劍道dropdownlists。爲此,我爲所有元素創建了HtmlHelpers,並在需要時調用它們。既然你使用的是kendo asp.net-mvc,我會推薦這樣看。

public static WindowBuilder GlobalKendoWindow(this HtmlHelper helper) 
    { 
     return helper.Kendo().Window() 
      .Draggable() 
      .Animation(true) 
      .Visible(false) 
      .AutoFocus(true) 
      .Modal(true) 
      .Scrollable(true) 
      .HtmlAttributes(new { @class = "atn-modal-container" }) 
      .Actions(actions => actions.Minimize().Close()) 
      .Deferred(); 
    } 

,呈現在我的HTML這樣的

@(Html.GlobalKendoWindow() 
    .Name("addCandidateDialog") 
    .Title(Html.GetResource(cps, "AddCandidateDialogTitle")) 
    .LoadContentFrom("AddCandidate", "Candidate") 
    .Events(events => events.Open("athena.addCandidacy.onAddCandidateOpen").Close("athena.addCandidacy.onAddCandidateClose")) 
) 
+1

看起來不錯,但我們現在更喜歡JS語法,因爲我們需要一些在這個Kendo MVC版本中設置困難(或者不可能)的行爲。感謝您的建議,希望有一天我會使用它。 –