【问题标题】:ExtJS how to dynamically create widgets and layouts from a stringExtJS 如何从字符串动态创建小部件和布局
【发布时间】:2013-10-18 03:28:44
【问题描述】:

ExtJS 库中是否有可能基于某些字符串表示创建布局?

假设我有一个服务器调用,它用以下数据填充响应 - "Ext.create('Ext.Button', {xtype: 'button', text: 'button form server'});" 这实际上是创建按钮的有效 javascript 代码(正在输入在源代码编辑器中它会创建一个按钮)。

有没有办法获得这样的字符串响应并完全基于该字符串构造 extjs 的按钮?

例如:ajax的response.responseText等于Ext.create('Ext.Button', {xtype: 'button', text: 'button form server'});。我需要的是一个像 Ext.someMagicStuff(response.responseText) 这样的组件,它可以理解我传递的字符串并创建一个按钮。

非常感谢您的时间和任何想法!

【问题讨论】:

    标签: string templates extjs widget render


    【解决方案1】:

    对于已定义的类型,这并不复杂。只需返回一个有效的配置,如

    {xtype: 'button', text: 'button form server'}
    

    作为 JSON 并使用

    解析答案
    Ext.ComponentManager.create(Ext.decode(response.responseText));
    

    组件管理器会自动查找xtype并返回创建的实例

    【讨论】:

    • 好建议,谢谢!但是,在尝试这样做时,会发生一个 javaScript 错误 - 它告诉 - “无法从空值或未确定的值获取子字符串属性”。 (ext-all-debug.js:5057 - 尝试解析命名空间)...
    • @user623417 如果没有一些代码示例,我无法告诉你任何事情。请发布服务器响应以及您处理它的位置以获得任何进一步的帮助。
    • 是的)所以,他们在这里 - 这就是我尝试获取和呈现服务器响应的方式:成功:function(response){ var panel = Ext.getCmp('panelChoose'); panel.add(Ext.ComponentManager.create(response.responseText));面板.doLayout(); }
    • 这是服务器创建响应的方式: PrintWriter outWriter = response.getWriter(); response.setContentType("text/html"); String outObj = "{xtype: 'button', text: '来自服务器的按钮'}"; outWriter.println(outObj); outWriter.close();
    • 以下是 ext-all-debug.js 文件中发生 NPE 的行 if (name === from || name.substring(0, from.length) === from) .. .. 其中 name 是传递给函数的命名空间变量 - parseNamespace: function(namespace)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多