Ext.ComponentLoader lazy loading html+js within component

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 14:40:07

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