【问题标题】:Extjs 5, data model association & load nested dataExtjs 5、数据模型关联&加载嵌套数据
【发布时间】:2015-01-25 19:14:46
【问题描述】:

试图完成这项工作....

我想在两个对象模型上加载嵌套数据

Ext.application({
name : 'MyApp',

launch : function() {


    Ext.define('MyApp.model.Address', {
        extend: 'Ext.data.Model',

        entityName: 'Address',

        fields: [ 

            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'addressLine',
                type: 'string'
            },
            {
                name: 'city',
                type: 'string'
            },
            {
                name: 'created',
                type: 'date',
                dateFormat: 'time',
                persist: false
            }
        ]
    });


    Ext.define('MyApp.model.User', {
        extend: 'Ext.data.Model',

        entityName: 'User',

        fields: [ 
            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'address',
                reference: 'Address'
            },
            {
                name: 'name',
                type: 'string'
            },
            {
                name: 'lastname',
                type: 'string'
            },
            {
                name: 'created',
                type: 'date',
                dateFormat: 'time',
                persist: false
            }
        ]
    });


    var user = new MyApp.model.User({
        "id": 1,
        "name": "Pedro",
        "lastname": "Carbonell",
        "address": {
            "id": 1,
            "addressLine": "Bailen 22",
            "city": "Barcelona",
            "created": 1420668866000
        },
        "created": 1420668866000
    });        

    console.info(user);
    console.info(user.getAddress());

}});

创建用户时没有错误的结果,但是当我通过 user.getAddress() 访问关联数据时,它返回了一个异常:

 Uncaught Error: The model ID configured in data ("[object Object]") has been rejected by the int field converter for the id fieldext-all-debug.js

尝试在模型定义上定义像内存或本地存储这样的代理,但结果是一样的。

分机:https://fiddle.sencha.com/#fiddle/h2d

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript extjs extjs5


    【解决方案1】:

    解决了,但只找到这个解决方案:当使用loadRawData...

        var store = new Ext.data.Store({
            model: MyApp.model.User
        });
    
        store.loadRawData({
            "id": 1,
            "name": "Pedro",
            "lastname": "Carbonell",
            "address": {
                "id": 1,
                "addressLine": "Bailen 22",
                "city": "Barcelona",
                "created": 1420668866000
            },
            "created": 1420668866000
        });        
    
        console.info(store.first());
        console.info(store.first().getAddress());
    

    这个新小提琴的样本:https://fiddle.sencha.com/#fiddle/h4e

    你说得对,ext有点不稳定,非常....

    【讨论】:

      【解决方案2】:

      我一直在玩弄你小提琴中的代码,但目前还无法让关联以官方方式工作。

      我使用此代码模拟了功能:

      Ext.define('MyApp.model.User', {
              extend: 'Ext.data.Model',
      
              fields: [{
                  name: 'id',
                  type: 'int'
              }, {
                  name: 'name',
                  type: 'string'
              }, {
                  name: 'lastname',
                  type: 'string'
              }, {
                  name: 'created',
                  type: 'date',
                  dateFormat: 'time',
                  persist: false
              }],
      
              getAddress: function() {
                  if ('undefined' === this.data.address) {
                      return null;
                  }
                  return Ext.create('Address', this.data.address);
              }
          });
      

      基本上我已经删除了关联并创建了一个自定义函数来根据传入的原始数据创建模型记录,如果地址数据不存在而不是 null,您也可以返回一个新的空模型,我使用 null 因为它更容易确定您是否有有效的地址记录。

      正如已经提到的 - 这不是官方的方法,我会再玩一次小提琴,一旦找到更好的解决方案,这可能会有所帮助。

      【讨论】:

      • 是的,非常感谢,这个解决方案对我有用,但我想了解为什么官方方法不起作用。
      • 我仍在调查,老实说,众所周知,ExtJs 关联有点不稳定或至少难以习惯,只需查看here 上关于它们的所有帖子。当我有更多信息时,我会更新答案。它似乎不喜欢你在两者上都使用id:这一事实,我将地址一更改为addressId,它克服了那个错误,但仍然没有加载数据。
      • 已解决,但仅当我使用 loadRawData... 你可以在这个新小提琴中看到:https://fiddle.sencha.com/#fiddle/h4e... 你说得对,ext 有点不稳定,非常... .
      【解决方案3】:

      使用原始代码,我做了一些修改,现在它似乎可以工作了。

      Ext.application({
      name : 'MyApp',
      
      launch : function() {
      
      
          Ext.define('MyApp.model.Address', {
              extend: 'Ext.data.Model',
      
              //entityName: 'Address',
      
              fields: [ 
      
                  {
                      name: 'id',
                      type: 'int'
                  },
                  {
                      name: 'addressLine',
                      type: 'string'
                  },
                  {
                      name: 'city',
                      type: 'string'
                  },
                  {
                      name: 'created',
                      type: 'date',
                      dateFormat: 'time',
                      persist: false
                  }
              ],
      
              hasMany: 'User'
          });
      
      
          Ext.define('MyApp.model.User', {
              extend: 'Ext.data.Model',
      
              //entityName: 'User',
      
              fields: [ 
                  {
                      name: 'id',
                      type: 'int'
                  },
                  {
                      name: 'name',
                      type: 'string'
                  },
                  {
                      name: 'lastname',
                      type: 'string'
                  },
                  {
                      name: 'created',
                      type: 'date',
                      dateFormat: 'time',
                      persist: false
                  }
              ], 
      
              hasMany: { model: 'Address', name: 'Address' }
          });
      
      
          var user = new MyApp.model.User({
              "id": 1,
              "name": "Pedro",
              "lastname": "Carbonell",
              "address": {
                  "id": 1,
                  "addressLine": "Bailen 22",
                  "city": "Barcelona",
                  "created": 1420668866000
              },
              "created": 1420668866000
          });        
      
          console.info(user);
          console.info(user.data.address);
      
      }
      });
      

      【讨论】:

      • 它似乎不起作用,这段代码不会产生任何关联......使用“user.data.address”,您正在访问未解析的原始对象。查找 user.data.address.created (它没有解析到像 user.get('created') 这样的日期对象......没有生成关联(你不能通过 user.getAdress().get('created ')...)。感谢您的尝试。
      【解决方案4】:

      这是你所追求的吗?我在用户模型上手动设置地址。不理想,但它被正确解释为记录。

          Ext.define('MyApp.model.Address', {
              extend: 'Ext.data.Model',
      
              entityName: 'Address',
      
              fields: [ 
      
                  {
                      name: 'id',
                      type: 'int'
                  },
                  {
                      name: 'addressLine',
                      type: 'string'
                  },
                  {
                      name: 'city',
                      type: 'string'
                  },
                  {
                      name: 'created',
                      type: 'date',
                      dateFormat: 'time',
                      persist: false
                  }
              ]
          });
      
      
          Ext.define('MyApp.model.User', {
              extend: 'Ext.data.Model',
      
              entityName: 'User',
      
              fields: [ 
                  {
                      name: 'id',
                      type: 'int'
                  },
                  {
                      name: 'addressId',
                      reference: 'Address'
                  },
                  {
                      name: 'name',
                      type: 'string'
                  },
                  {
                      name: 'lastname',
                      type: 'string'
                  },
                  {
                      name: 'created',
                      type: 'date',
                      dateFormat: 'time',
                      persist: false
                  }
              ]
          });
      
      
          var user = new MyApp.model.User({
              "id": 1,
              "name": "Pedro",
              "lastname": "Carbonell",
              "created": 1420668866000
          });        
      
          var addr  = new MyApp.model.Address({
                  "id": 1,
                  "addressLine": "Bailen 22",
                  "city": "Barcelona",
                  "created": 1420668866000
          });
          user.setAddress(addr);
          console.info(user);
          console.info(user.getAddress());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多