2017-08-29 85 views
1

我有以下模型並存儲在我的應用程序中。Extjs 4.1:Ext.data.Store記錄未加載到第二個Ext.data.Store

我的問題是,當我試圖加載記錄到第二個商店它不起作用。我嘗試了不同的商店方法,並且沒有任何內容(Store Manual)。

在我的應用程序中,第一個商店記錄被加載到控制器中,其中Ajax調用收到data.products變量。

任何想法我做錯了什麼?

PS:我使用ExtJS的4.1

Fiddle sencha

Ext.define('App.model.Product', { 
 
    extend: 'Ext.data.Model', 
 
    alias: 'model-product', 
 
    idgen: 'sequential', 
 
    fields: [ 
 
     { name: 'available', type: 'boolean', useNull: false, defaultValue: true }, 
 
     { name: 'country', type: 'int', useNull: false }, 
 
     { name: 'key', type: 'string', useNull: false }, 
 
     { name: 'name', type: 'string', useNull: false } 
 
    ], 
 
    proxy: { 
 
     type: 'memory', 
 
     reader: { 
 
      type: 'json', 
 
      root: 'products' 
 
     } 
 
    } 
 
}); 
 

 
Ext.define('App.store.Product', { 
 
    extend: 'Ext.data.Store', 
 
    autoLoad: true, 
 
    autoSync: true, 
 
    groupField: 'id', 
 
    countryFilter: function(countryId) { 
 
     this.clearFilter(); 
 
     this.filter('country', countryId); 
 
     return this; 
 
    }, 
 
    getRecordsForCountry: function (countryId) { 
 
     var records = []; 
 
     this.findBy(function (record) { 
 
      if (record.get('country') === countryId) { 
 
       records.push(record.copy()); 
 
      } 
 
     }); 
 
     return records; 
 
    }, 
 
    model: 'App.model.Product', 
 
    sorters: [ { 
 
     property: 'key', 
 
     direction: 'ASC' 
 
    } ], 
 
    sortOnLoad: true 
 
}); 
 

 
Ext.onReady(function() { 
 
    var data = { 
 
     products: [{ 
 
      country: 1, 
 
      key: 'test1', 
 
      name: 'Test1' 
 
     }, { 
 
      country: 2, 
 
      key: 'test2', 
 
      name: 'Test2' 
 
     }, { 
 
      country: 3, 
 
      key: 'test3', 
 
      name: 'Test3' 
 
     }] 
 
    }; 
 

 
    var store = Ext.create('App.store.Product'); 
 
    store.loadRawData(data, false); 
 
    
 
    var store2 = Ext.create('App.store.Product'), 
 
    records = store.getRecordsForCountry(1); 
 
    store2.add(records); 
 
    //tried also: 
 
    //store2.loadRecords(records); 
 
    //store2.loadData(records); 
 
    //store2.loadRawData(records); 
 

 
    var combobox = Ext.create('Ext.form.field.ComboBox', { 
 
     queryMode: 'local', 
 
     forceSelection: true, 
 
     displayField: 'name', // <-- Add this 
 
     valueField: 'key', 
 
     renderTo: Ext.getBody(), 
 
     store: store 
 
    }); 
 
    var combobox2 = Ext.create('Ext.form.field.ComboBox', { 
 
     queryMode: 'local', 
 
     forceSelection: true, 
 
     displayField: 'name', // <-- Add this 
 
     valueField: 'key', 
 
     renderTo: Ext.getBody(), 
 
     store: store2 
 
    }); 
 
});
<link href="http://docs.sencha.com/extjs/4.1.1/extjs-build/resources/css/ext-all.css" rel="stylesheet"/> 
 
<script src="http://cdn.sencha.com/ext/gpl/4.1.1/ext-all.js"></script>

回答

1

顯然,這兩個設置:

autoLoad: true, 
autoSync: true 

螺絲全倉了,並調用load空記錄(觸發loadRawData,loadRecords, clearFilter,filter)。

將這兩個設置爲false之後,加載僅在明確調用加載方法時發生。

Ext.define('App.model.Product', { 
 
    extend: 'Ext.data.Model', 
 
    alias: 'model-product', 
 
    idgen: 'sequential', 
 
    fields: [ 
 
     { name: 'available', type: 'boolean', useNull: false, defaultValue: true }, 
 
     { name: 'country', type: 'int', useNull: false }, 
 
     { name: 'key', type: 'string', useNull: false }, 
 
     { name: 'name', type: 'string', useNull: false } 
 
    ], 
 
    proxy: { 
 
     type: 'memory', 
 
     reader: { 
 
      type: 'json', 
 
      root: 'products' 
 
     } 
 
    } 
 
}); 
 

 
Ext.define('App.store.Product', { 
 
    extend: 'Ext.data.Store', 
 
    autoLoad: false, 
 
    autoSync: false, 
 
    groupField: 'id', 
 
    countryFilter: function(countryId) { 
 
     this.clearFilter(); 
 
     this.filter('country', countryId); 
 
     return this; 
 
    }, 
 
    getRecordsForCountry: function (countryId) { 
 
     var records = []; 
 
     this.findBy(function (record) { 
 
      if (record.get('country') === countryId) { 
 
       records.push(record.copy()); 
 
      } 
 
     }); 
 
     return records; 
 
    }, 
 
    model: 'App.model.Product', 
 
    sorters: [ { 
 
     property: 'key', 
 
     direction: 'ASC' 
 
    } ], 
 
    sortOnLoad: true 
 
}); 
 

 
Ext.onReady(function() { 
 
    var data = { 
 
     products: [{ 
 
      country: 1, 
 
      key: 'test1', 
 
      name: 'Test1' 
 
     }, { 
 
      country: 2, 
 
      key: 'test2', 
 
      name: 'Test2' 
 
     }, { 
 
      country: 3, 
 
      key: 'test3', 
 
      name: 'Test3' 
 
     }] 
 
    }; 
 

 
    var store = Ext.create('App.store.Product'); 
 
    store.loadRawData(data, false); 
 
    
 
    var store2 = Ext.create('App.store.Product'), 
 
    records = store.getRecordsForCountry(1); 
 
    store2.add(records); 
 
    //tried also: 
 
    //store2.loadRecords(records); 
 
    //store2.loadData(records); 
 
    //store2.loadRawData(records); 
 

 
    var combobox = Ext.create('Ext.form.field.ComboBox', { 
 
     queryMode: 'local', 
 
     forceSelection: true, 
 
     displayField: 'name', // <-- Add this 
 
     valueField: 'key', 
 
     renderTo: Ext.getBody(), 
 
     store: store 
 
    }); 
 
    var combobox2 = Ext.create('Ext.form.field.ComboBox', { 
 
     queryMode: 'local', 
 
     forceSelection: true, 
 
     displayField: 'name', // <-- Add this 
 
     valueField: 'key', 
 
     renderTo: Ext.getBody(), 
 
     store: store2 
 
    }); 
 
});
<link href="http://docs.sencha.com/extjs/4.1.1/extjs-build/resources/css/ext-all.css" rel="stylesheet"/> 
 
<script src="http://cdn.sencha.com/ext/gpl/4.1.1/ext-all.js"></script>