【问题标题】:Sencha Touch 2: Two REST request in the same StoreSencha Touch 2:同一个 Store 中的两个 REST 请求
【发布时间】:2015-01-11 11:51:54
【问题描述】:

是否可以在同一个商店中以某种方式包含多个 rest/json 请求?

例如,我有这个返回 json 对象的 api:

http://api1.domain.com

{"name":"john"},{"name:"harry"}

http://api2.domain.com

{"name":"peter"},{"name:"fred"}

我想将 api1 和 api2 的结果合并到一个存储中,以便在一个视图中处理所有连接。结果:

{"name":"john"},{"name:"harry"},{"name":"peter"},{"name:"fred"}

或者可能没有连接,但我可以在一个视图中同时调用这两个 api: 约翰 哈利 彼得 弗雷德

这可能吗?

我正在尝试这个没有成功:

Ext.application({
    name: 'sencha',
    views: ['Main1'],
    models:['AdModel'],
    stores:['AdStore1','AdStore2'],
    launch: function () {

    var store1Obj = Ext.getStore('AdStore1');
    var store2Obj = Ext.getStore('AdStore2');

    store1Obj.each(function(record){
        var copiedRecord = record.copy();
    store2Obj.add(copiedRecord);
    });

    Ext.create('Ext.DataView', {
        fullscreen: true,
        store: store2Obj,
        itemTpl: '<tpl for="."><div><strong>{name}</strong></div></tpl>'
        });
    }

});

【问题讨论】:

    标签: ajax json rest extjs sencha-touch


    【解决方案1】:

    首先,您对store http://docs-origin.sencha.com/touch/2.4/2.4.1-apidocs/#!/api/Ext.data.Store-method-eacheach 方法使用了错误的语法

    那么在同一个 store 中添加 2 个不同的数据源就很简单了。

    Ext.application({
        name: 'Fiddle',
    
        launch: function() {
    
            // Set up a model to use in our Store
            Ext.define("User", {
                extend: "Ext.data.Model",
                config: {
                    fields: [{
                        name: "name",
                        type: "string"
                    }]
                }
            });
    
            // Setup the stores
            var myStore1 = Ext.create("Ext.data.Store", {
                model: "User",
                data: [{
                    "name": "john"
                }, {
                    "name": "harry"
                }],
                autoLoad: true
            });
    
            var myStore2 = Ext.create("Ext.data.Store", {
                model: "User",
                data: [{
                    "name": "peter"
                }, {
                    "name": "fred"
                }],
                autoLoad: true
            });
    
            // Loop through each record of store2 adding it to store1
            myStore2.each(function(item, index, length) {
                myStore1.add(item);
            });
    
            Ext.create("Ext.List", {
                fullscreen: true,
                store: myStore1,
                itemTpl: "{name}"
            });
        }
    });
    

    演示:https://fiddle.sencha.com/#fiddle/g6n

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 2014-02-06
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多