When it comes to the Ext JS library, there is quite a lot of negativity from the experts: hard, expensive, buggy. As a rule, most of the problems associated with the inability to cook it. A project correctly assembled using Sencha Cmd with all css, pictures weighs in production in the region of 1Mb, which is comparable to the same Angular. And the glitches are not much more ...Ext.define('Module.message.model.Message', { .... /* scope:server */ ,async newMessage() { ......... this.fireEvent('newmessage', data); ...... } ... }) git clone https://github.com/Kolbaskin/extjs-backend-example cd extjs-backend-example npm i node server // http- express // Ext JS express const express = require('express'); const staticSrv = require('extjs-express-static'); const app = express(); const bodyParser = require('body-parser'); // global = { config: require('config') } // Ext JS require('extjs-on-backend')({ // express app, // wsClient: 'Base.wsClient' }); // Ext.Loader.setPath('Api', 'protected/rest'); Ext.Loader.setPath('Base', 'protected/base'); Ext.Loader.setPath('Www', 'protected/www'); // http app.use( bodyParser.json() ); app.use(bodyParser.urlencoded({ extended: true })); // Ext JS app.use('/api/auth', Ext.create('Api.auth.Main')); app.use('/www/auth', Ext.create('Www.login.controller.Login')); // app.use(staticSrv(__dirname + '/static')); // const server = app.listen(3000, () => { console.log('server is running at %s', server.address().port); }); app.use('/api/auth', Ext.create('Api.auth.Main')); app.use('/www/auth', Ext.create('Www.login.controller.Login')); Ext.define('Api.auth.Main', { extend: 'Api.Base', // // routes: [ { path: '/', get: 'login'}, { path: '/restore', post: 'restoreLogin' }, { path: '/registration', post: 'newuser'}, { path: '/users', get: 'allUsers'} ] // : // {query: <...>, params: <...>, body: <...>} ,async login(data) { return {data:[{ id:1, subject: 111, sender:222, }]} } ,async restoreLogin() { ... } ,async newuser() { ... } ,async allUsers() { .... } }) Ext.define('Www.login.controller.Login', { // "" : // , .. extend: 'Www.Base' // // , .. ,baseTpl: 'view/inner' // // , ,loginFormTpl: 'login/view/login' // ,routes: [ { path: '/', get: 'loginForm', post: 'doLogin'} ] // html // ,async loginForm () { return await this.tpl(this.loginFormTpl, { pageTitle: 'Login page', date: new Date() }); } ,async doLogin (params, res) { if(params.body.name && /^[a-z0-9]{2,10}$/i.test(params.body.name)) { this.redirect(`/index.html?name=${params.body.name}`, res); return; } return await this.tpl(this.loginFormTpl, { pageTitle: 'Login page', date: new Date() }); } }) <h2>{pageTitle} (date: {[Ext.Date.format(values.date,'dmY')]})</h2> <form method="post"> <input name="name" placeholder="name"> <button type="submit">enter</button> </form> // Ext.Loader.setConfig({ enabled: true, paths: { "Core": "app/core", "Admin": "app/admin", "Module": "app/admin/modules", "Ext.ux": "ext/ux" } }); // this.token = Ext.data.identifier.Uuid.createRandom()(); // // () // ( ) Ext.WS = Ext.create('Core.WSocket', { token: this.token, user: new URLSearchParams(document.location.search).get("name") }); // Ext.application({ name: 'Example', extend: 'Ext.app.Application', requires: ['Admin.*'], autoCreateViewport: 'Admin.view.Viewport' }) Ext.define('Module.users.store.UsersStore', { extend: 'Ext.data.Store' ,autoLoad: true ,total: 0 ,constructor() { // this.dataModel = Ext.create('Module.users.model.UserModel'); // this.dataModel.on({ add: (records) => { this.onDataAdd(records) }, remove: (records) => { this.onDataRemove(records) } }) this.callParent(arguments) } // load ,async load() { // const data = await this.dataModel.$read(); // this.total = data.total; // UI this.loadData(data.data); } ,getTotalCount() { return this.total; } // ,onDataAdd(records) { this.add(records[0]); } // -- ,onDataRemove(records) { this.remove(this.getById (records[0].id)) } }); Ext.define('Module.users.model.UserModel', { extend: 'Core.data.DataModel' /* scope:client */ ,testClientMethod() { ... } ,testGlobalMethod() { ... } /* scope:server */ ,privateServerMethod() { .... } /* scope:server */ ,async $read(params) { // redis const keys = await this.getMemKeys('client:*'); let data = [], name; for(let i = 0;i<keys.length;i++) { // name = await this.getMemKey(keys[i]); if(name) { data.push({ id: keys[i].substr(7), name }) } } // return { total: data.length, data } } }) Ext.define('Base.wsClient', { extend: 'Core.WsClient' // ,usersModel: Ext.create('Module.users.model.UserModel') // ,async onStart() { // "add" this.usersModel.fireEvent('add', 'all', [{id: this.token, name: this.req.query.user}]); // redis await this.setMemKey(`client:${this.token}`, this.req.query.user || ''); // "" , // await this.queueProcess(`client:${this.token}`, async (data, done) => { const res = await this.prepareClientEvents(data); done(res); }) } // ,onClose() { // "remove" this.usersModel.fireEvent('remove', 'all', [{id: this.token, name: this.req.query.user}]) this.callParent(arguments); } }) Ext.define('Module.messages.view.FormController', { extend: 'Ext.app.ViewController' ,init(view) { this.view = view; // this.model = Ext.create('Module.messages.model.Model'); // this.msgEl = this.view.down('[name=message]'); // this.usersGrid = Ext.getCmp('users-grid') // "" this.control({ '[action=submit]' : {click: () => {this.newMessage() }} }) } // ,newMessage() { let users = []; // const sel = this.usersGrid.getSelection(); if(sel && sel.length) { sel.forEach((s) => { users.push(s.data.id) }) } // if(users.length && users.indexOf(Ext.WS.token) == -1) users.push(Ext.WS.token); // this.model.$newmessage({ to: users, user: Ext.WS.user, message: this.msgEl.getValue() }) // this.msgEl.setValue(''); } }); Ext.define('Module.messages.model.Model', { extend: 'Core.data.DataModel' /* scope:server */ ,async $newmessage(data) { const msg = { user: data.user, message: data.message } if(data.to && Ext.isArray(data.to) && data.to.length) { this.fireEvent('newmessage', data.to, msg); } else { this.fireEvent('newmessage', 'all', msg); } return true; } }) Ext.define('Module.messages.store.MessagesStore', { extend: 'Ext.data.Store', fields: ['user', 'message'], constructor() { // Ext.create('Module.messages.model.Model', { listeners: { newmessage: (mess) => { this.add(mess) } } }) this.callParent(arguments); } }); Source: https://habr.com/ru/post/431180/
All Articles