如何在没有循环引用的情况下在控制器之间共享对象/方法? [英] How to share objects/methods between controllers without circular references?

查看:70
本文介绍了如何在没有循环引用的情况下在控制器之间共享对象/方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常简单的问题.当前,当我需要在整个应用程序中访问对象的方法时该怎么做,我在app.js

Pretty straightforward question. Currently, what I do when I need to access objects' methods throughout most of the application, I do this in app.js

Ext.define('Utils', {
    statics: {
        myMethod: function() {
            return 'myResult'; 
        }
    }
});

这可行,但是当我构建应用程序时,会收到关于循环引用的警告(当然,app.js需要所有其他控制器类,但随后这些类会引用回app.js).

This works, but when I build the application, I get a warning about a circular reference (app.js of course needs all the other controller classes, but then said classes refer back to app.js).

我考虑过使用一个包含.js文件的程序包,该文件包含所有对象/方法,但是有时,在这些方法中,我需要访问Ext命名空间,以使其无法正常工作.

I thought of using a package including a .js file that has all the objects/methods, but sometimes, within these methods I'll need access to the Ext namespace so that won't work.

还有更好的方法吗?

谢谢!

推荐答案

您不应在app.js内定义类Utils.每个Ext.define语句应位于其自己的文件中.另外,类名还应以您的应用程序名称为前缀.

You should not define the class Utils inside app.js. Each Ext.define statement should be in it's own file. Also the classname should be prefixed with your app name.

Ext.define('MyApp.Utils', {
    statics: {
        myMethod: function() {
            return 'myResult'; 
        }
    }
});

因此,应该在文件app/Utils.js中找到

.将源代码编译为已编译的app.js时,Sencha Cmd将注意最终文件中的正确顺序. requiresuses指令为您提供了足够的灵活性来定义类之间的任何依赖关系.

should therefore be found in the file app/Utils.js. When compiling your sources into a compiled app.js, Sencha Cmd will take care of the proper ordering in the final file. The requires and uses directives give you enough flexibility to define any dependences between your classes.

阅读有关MVC的所有文档,以获得清晰的了解.

Read all the docs about MVC to get a clear understanding.

这篇关于如何在没有循环引用的情况下在控制器之间共享对象/方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆