Ext.ComponentLoader lazy loading html+js within co

2019-05-29 16:47发布

问题:

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();
    }
});