Ext.ComponentLoader lazy loading html+js within component

廉价感情. 提交于 2020-01-03 02:25:18

问题


I have four files:

layout.html //contains ext resources
layout.js //contains my panel
partial.html //contains <script src="partial.js">
partial.js //contains a component

I have this panel configured in my layout.js:

var myPanel = Ext.widget('panel',{
    title: 'Load HTML into panel',
    html: 'initial',
    width: 300,
    height: 300,
    loader: {
        url: 'partial.html',
        renderer: 'html',
        scripts: true,
        autoLoad: true
    },
    renderTo: Ext.getBody()
});

And this code in my partial.js

Ext.onReady(function(){

    var myDynPanel = Ext.widget('panel',{
        title: 'Is this working',
        html: 'Dynamic test',
        //renderTo: 'myDynPnl'
    });

});

I want to render myDynPanel inside my myPanel. I am aware that the renderer config on the loader can be set to component, but I'm working with C#.NET MVC and I'm getting partial views results as HTML.

My solution now is create a <div id="myDynPnl"></div> inside partial.html and render the dynamic panel to the div. But I don't want to use id in my page.

What is the best way to accomplish this. Thanks in advance.

using: ExtJS 4.1.2


回答1:


After a month we created a solution :).

Have a question? Ask it!

PartialViewLoader.js

Ext.define('PartialViewLoader', {
    mixins: {
        observable: 'Ext.util.Observable'
    },
    constructor: function(config) {
        this.mixins.observable.constructor.call(this, config);
        this.addEvents(
            'loaded'
        );
    },
    load: function(config) {
        var component;

        if (this.url == undefined) {
            component = this.loadFunction(config);
            this.fireEvent('loaded', component);
        }
        else {
            Ext.Loader.loadScript({
                url: this.url,
                onLoad: function() {
                    component = this.loadFunction(config);
                    this.fireEvent('loaded', component);
                },
                //onError: function(r) {},
                scope: this
            });
        }
    }
});

Index.js

Ext.define('MyView', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.myview',
});

Index.cshtml

@model MyNameSpace.MyModel
Ext.create('PartialViewLoader', {
    url: '~/Scripts/Index.js',
    loadFunction: function(config){
        return Ext.create('MyView', Ext.apply(config, {}));
    }
})

Js File

Ext.Ajax.request({
    scope: this,
    method: 'POST',
    url: '~/Views/Index', //controller action that returns a partial view (Index.cshtml)
    //params: {},
    success: function (response) {
        var loader = Ext.decode(response.responseText);

        loader.on('loaded', function (cmp) {
            //this.add(cmp); //Custom logic
        });
        loader.load();
    }
});


来源:https://stackoverflow.com/questions/12857923/ext-componentloader-lazy-loading-htmljs-within-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!