2014-04-02 38 views
1

我的綁定對象有setter和,而在柵格單元變更值,則設置器沒有得到調用。爲什麼?劍道網格更改不會反映在源對象

function PresonDetails(_contactName, _contactTitle, _country, _companyName) { 

Object.defineProperties(this, { 
    "ContactName": { 
     get: function() { 
      return this._contactName; 
     }, 
     set: function (value) { 
      alert("New ContactName is :" + value) 
      this._contactName = value; //Setter is not get called when i change the value in grid. 
     }, 
     enumerable: true, 
     configurable: true 
    }, 
     "ContactTitle": { 
     get: function() { 
      return this._contactTitle; 
     }, 
     set: function (value) { 
      this._contactTitle = value; 
     }, 
     enumerable: true, 
     configurable: true 
    }, 
     "Country": { 
     get: function() { 
      return this._country; 
     }, 
     set: function (value) { 
      this._country = value; 
     }, 
     enumerable: true, 
     configurable: true 
    }, 
     "CompanyName": { 
     get: function() { 
      return this._companyName; 
     }, 
     set: function (value) { 
      this._companyName = value; 
     }, 
     enumerable: true, 
     configurable: true 
    } 
}); 

this.ContactName = _contactName; 
this.ContactTitle = _contactTitle; 
this.Country = _country; 
this.CompanyName = _companyName; 
} 

(function() { 
    var details = []; 
    details.push(new PresonDetails("ContactName1", "ContactTitle", "USA", "MICro")); 


    var $grid = $('#grid'); 
    $grid.kendoGrid({ 
     scrollable: true, 
     dataSource: details, 
     groupable: false, 
     sortable: false, 
     editable: true, 
     columns: [{ 
      field: "ContactName", 
      title: "Contact Name", 
      width: 200 
     }, { 
      field: "ContactTitle", 
      title: "Contact Title", 
      width: 250 
     }, { 
      field: "CompanyName", 
      title: "Company Name" 
     }, { 
      field: "Country", 
      width: 150, 
     }] 
    }); 
})(); 

這裏是demo

回答

2

你是對的,那就不叫這是正確的。

嗯,我的意思是,當你綁定一個數組(這是你已經定義了什麼是details),劍道UI將其轉換爲ObservableObject -actually在內部使用的ObservableArray那就是ObservableObjects陣列的DataSource - 但這並不意味着使用你的對象定義。它的作用是創建一個ObservableObject您的數據,但在內部使用自己的Model

因此,你應該做的是爲你的對象定義一個Model,那裏你可以定義一個set和一個get方法。