简体   繁体   English

我将如何从json文件创建模型? (ExtJS的)

[英]How I'll create a model from json file? (ExtJS)

This the model that I want to create using json file 这是我想用json文件创建的模型

Ext.define('Users', {
    extend: 'Ext.data.Model',
    fields: [{name: 'user_id',  type: 'int'},
    {name: 'user_name',  type: 'string'}]
});

What do I have to do in order to automatically create this model, based on the content of a json response from the server? 基于来自服务器的json响应的内容,我需要做什么才能自动创建此模型?

In order to have the model created automatically, you need to include the metaData field with your Json data. 为了自动创建模型,您需要将metaData字段包含在Json数据中。 metaData can be used to describe all of the fields for the Model. metaData可用于描述模型的所有字段。

In the ExtJS 4.1 documentation - Ext.data.reader.Json has a section called Response MetaData which describes basic use of this feature. 在ExtJS 4.1文档中 - Ext.data.reader.Json有一个名为Response MetaData的部分,它描述了此功能的基本用法。

You should be able to pull down some json with fields and or some format that can be transformed into that format pretty easily. 您应该能够使用字段和/或某些格式下拉一些json,这些格式可以很容易地转换为该格式。

  1. Make call to service to get model's fields. 调用服务以获取模型的字段。 Might need to define some chain that first calls model service and performs subsequent steps after. 可能需要定义一些首先调用模型服务并在之后执行后续步骤的链。

  2. Build model's field array w/ fields results from #1. 构建模型的字段数组w /字段来自#1。 May need to transform data based on response in #1. 可能需要根据#1中的响应来转换数据。

    var fields = response.fields;

  3. Define model based on fields in Store's constructor 根据Store的构造函数中的字段定义模型

     var store = Ext.create('Ext.data.Store', { constructor: function () { var model = Ext.define("Users", { extend: "Ext.data.Model", fields: fields }); this.model = model.$className; this.callParent(arguments); } }); 

I only use the jsonp, which loads an json file and parses it automatically, don't know if Ext.Ajax does this, too. 我只使用jsonp,它加载一个json文件并自动解析它,不知道Ext.Ajax是否也这样做。

But you would do something like this: 但你会做这样的事情:

definition.json: definition.json:

{
  "name": "User",
  "fields": [
    { "name": "user_id"  , "type": "int"    },
    { "name": "user_name", "type": "string" }
  ]
}

load it: 加载它:

Ext.Ajax.request({
  url    : "..../definition.json"
  success: function( res ) {
    Ext.define( res.name, {
      extend: 'Ext.data.Model',
      fields: res.fields
    }, function() {
      Ext.create( 'somestore', { model: res.name });
    });
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM