【问题标题】:How to add record to ExtJS store "persistently"?如何“持久地”将记录添加到 ExtJS 存储?
【发布时间】:2014-03-11 08:03:28
【问题描述】:

有两个相同模型的商店:inputStoreoutputStoreConcepts 面板中的网格显示来自 inputStore 的数据。 当用户选中该网格中的一行时(通过 Ext.selection.Checkbox.Model),该记录将添加到 outputStore 中。

我已设法做到这一点,但无法持久将记录添加到 outputStore。这是我的做法:

我向作为网格一部分的 Ext.selection.Checkbox.Model 添加了一个侦听器:

selModel: Ext.create('Ext.selection.CheckboxModel', {
listeners: {
    select: {
        fn: me.onCheckboxModelSelect,
        scope: me
    }
}
})

然后将记录添加到 outputStore 中,效果很好。

onCheckboxModelSelect: function(rowmodel, record, index, eOpts) {
    var griddd = Ext.ComponentQuery.query('#outputGridPanel')[0];
    griddd.getStore().add(record);
}

我尝试过 Store.sync()Store.commitChanges()Store.save()

你好!将两个存储从内联数据存储更改为带有代理的 json 存储。例如这里是 outputStore

Ext.define('justConcepts.store.outputStore', {
extend: 'Ext.data.Store',

requires: [
    'justConcepts.model.ConceptsModel',
    'Ext.data.proxy.Ajax',
    'Ext.data.reader.Json'
],

constructor: function(cfg) {
    var me = this;
    cfg = cfg || {};
    me.callParent([Ext.apply({
        autoLoad: true,
        autoSync: true,
        model: 'justConcepts.model.ConceptsModel',
        storeId: 'outputStore',
        proxy: {
            type: 'ajax',
            url: 'data/output.json',
            reader: {
                type: 'json',
                root: 'concepts'
            }
        }
    }, cfg)]);
}
});

这是 data/output.json 文件:

{
concepts: [
    {  
      "id": "00A212",  
      "name": "Pays",  
      "adjacencies": ["00A213", "01A455"]  
    },
    {  
      "id": "00A213",  
      "name": "Ville",  
      "adjacencies": ["00A212"]  
    },  
    {  
      "id": "01A455",  
      "name": "Fleuve", 
      "adjacencies": ["00A212"]  
    } 
]
}

griddd.getStore().add(record); 仍在工作,但无法持久,griddd.getStore().getProxy().getModel().save(); 不工作。

【问题讨论】:

  • 当您持续说下去时,我假设您的意思是它被通过网络推送到数据库或某种数据存储中。这家商店有代理设置来处理保存吗?
  • 只是一个内嵌数据的商店
  • 您希望它在哪里持续存在?这是一个静态的 json 文件。
  • @EvanTrimboli 不能写入那个 json 文件吗?
  • 不,不能。你能想象如果你可以向服务器发出请求并让它更新文件吗?

标签: javascript extjs crud


【解决方案1】:

您的问题可能出在模型定义中。

你试过了吗:

griddd.getStore().getProxy().getModel().save();

顺便说一句,Ext.data.Store.save() 方法不存在。我的意思是,我认为它已从 ExtJs 的 v4.x 版本中删除。

示例取自Sencha ExtJS Docs

//this is the model we will be using in the store
Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'id',    type: 'int'},
        {name: 'name',  type: 'string'},
        {name: 'phone', type: 'string', mapping: 'phoneNumber'}
    ]
});

//this data does not line up to our model fields - the phone field is called phoneNumber
var data = {
    users: [
        {
            id: 1,
            name: 'Ed Spencer',
            phoneNumber: '555 1234'
        },
        {
            id: 2,
            name: 'Abe Elias',
            phoneNumber: '666 1234'
        }
    ]
};

//note how we set the 'root' in the reader to match the data structure above
var store = Ext.create('Ext.data.Store', {
    autoLoad: true,
    model: 'User',
    data : data,
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            root: 'users'
        }
    }
});

你有所有的信息。 但是如果您需要具体的帮助,您应该发布您的所有代码,以便我们为您提供帮助!

【讨论】:

  • 您是否有使用代理定义的模型,或者您只是使用带有内联数据的存储?
  • 我已经试过了。调试器返回这个 'undefined'不是一个函数(评估'griddd.getStore().getProxy().getModel().save()') 但是save( ) 是 Ext.data.Model 的一个方法!所以也许 griddd.getStore().getProxy().getModel() 没有返回模型。我做到了: alert(griddd.getStore().getProxy().getModel()); 这是我得到的 JS 警报: function j() {return this.constructor.apply(this,arguments)||null;}
  • 只是一个内嵌数据的商店
  • 在内联数据的情况下谈论持久性无关紧要??
  • 在您的情况下是的,因为您没有Ext.data.proxy.Memory 如果您使用内存代理(或物理代理)创建模型,那么您实际上可以保存记录!
猜你喜欢
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多