【问题标题】:Extjs4 MVC, how get Model or View from a Controller class?Extjs4 MVC,如何从控制器类中获取模型或视图?
【发布时间】:2012-06-10 15:37:03
【问题描述】:
Ext.define('DigitalPaper.controller.Documents', {
 extend: 'Ext.app.Controller',

 views:  ['Documents'],
 stores: ['Documents'],
 models: ['Documents'],

 init: function() {
     console.log('[OK] Init Controller: Documents');
 } 
});

获取此控制器的模型或视图的功能是什么? 我试过了

  Ext.getModel('Documents');
  this.getModel('Documents');
  this.getModel();
  this.getDocumentsModel();

有什么建议吗?

【问题讨论】:

    标签: extjs view model extjs4


    【解决方案1】:

    找到的解决方案:

    在控制器中可以使用对视图的引用:

    refs: [{
        ref: 'viewDocuments', // will be create the method this.getViewDocuments();
        selector: 'Documents'
    }],
    

    因此您可以通过以下方式获取视图:

    this.getViewDocuments();
    

    【讨论】:

    • 似乎没有人说过“this.getViewDocuments();”在哪里需要 - 或者人们期望我使用它的上下文中的“这个”是什么:(
    • @PaulHutchinson 通过利用 ExtJS 控制器定义的 'refs' 部分,您可以创建对常用组件的快速引用。引用会根据为“ref”提供的文本自动创建“getter”方法。在此示例中,创建的 getter 是“getViewDocuments”。这可以在整个控制器中调用,其中“this”指的是控制器范围。在结果 Ext.ComponentQuery.query() 调用中使用“选择器”定义。查看 ComponentQuery.query() 的文档,了解选择器定义的强大功能!
    【解决方案2】:

    这应该可行:

    Ext.define('DigitalPaper.controller.Documents', {
     extend: 'Ext.app.Controller',
    
     views:  ['Documents'],
     stores: ['Documents'],
     models: ['Documents'],
    
     init: function() {
         console.log('[OK] Init Controller: Documents');
    
         // get references to view and model classes which can be used to create new instances
         console.log('View', this.getDocumentsView());
         console.log('Model', this.getDocumentsModel());
    
         // reference the Documents store
         console.log('Store', this.getDocumentsStore());
    
     } 
    

    });

    这些方法是由 Ext 控制器中创建 getter 的方法创建的。 http://docs.sencha.com/ext-js/4-0/source/Controller.html#Ext-app-Controller

    该方法如下所示:

    createGetters: function(type, refs) {
        type = Ext.String.capitalize(type);
        Ext.Array.each(refs, function(ref) {
            var fn = 'get',
                parts = ref.split('.');
    
            // Handle namespaced class names. E.g. feed.Add becomes getFeedAddView etc.
            Ext.Array.each(parts, function(part) {
                fn += Ext.String.capitalize(part);
            });
            fn += type;
    
            if (!this[fn]) {
                this[fn] = Ext.Function.pass(this['get' + type], [ref], this);
            }
            // Execute it right away
            this[fn](ref);
        },
        this);
    },
    

    【讨论】:

    • getDocumentsView() 不返回文档视图的实例
    • 好点。我用 cmets 更新了示例 sn-p,这些 cmets 记录了 getter 返回的内容。
    • 查看函数() 模型函数() 存储对象 { events={...}, data={...}, groupers={...}, altri elementi...} - > 我在控制台中得到了这个,它适用于商店,但不适用于视图和模型。我得到“函数()”
    【解决方案3】:

    Ext 控制器非常奇怪,因为给定控制器只有一个实例,无论您有多少相关的视图实例。在大多数 MVC 或 MVP 系统中,每个视图实例都有一个控制器实例。

    如果您计划使用多个视图实例,则不应在控制器中保留对这些视图的引用。

    您可能想要查看 Deft 的 ExtJs MVC 扩展,每个视图实例有一个控制器实例(加上依赖注入):

    http://deftjs.org/

    无论如何,controller.getView() 返回对视图类的引用,而不是对象实例。与 getModel() 相同。 getStore() 确实返回一个商店实例。

    在您的控制器中,您可以执行以下操作:

    this.viewInstance = this.getDocumentsView().create();
    

    我还建议您以单数形式命名您的模型。它不是文件。它是一个文档。

    【讨论】:

      【解决方案4】:

      getModel() 和 getView() 不返回控制器的模型/视图 - 它们在应用程序中返回这些的实例(如果不存在,它们将被实例化)。

      您可以简单地使用它来获取视图/模型名称:

      this.views[0]
      

      我不确定你在哪里使用你的获取(即 this.getModel('Details') ),但这些应该正确返回模型的实例(构造函数是唯一可能遇到问题的地方这些)。

      【讨论】:

      • 这个答案没有多大意义。
      • 是的,我一定误解了这个问题。你是对的,getModel 返回的是类而不是实例;这就是我们使用 .create(); 的原因
      猜你喜欢
      • 2012-04-09
      • 2012-12-02
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多