【问题标题】:javascript oop and thisjavascript oop 和这个
【发布时间】:2011-08-23 05:23:18
【问题描述】:

给定以下代码:

JE.events = {
  self: this,
  controller: {
    init: function(){            
        $(".monthheader").click(function () { 
            JE.events.model.get($(this).attr('title')); 
            return false;
        });

        return this;
    }
  },

  model: {
    get: function(monthnum){
        ...
    }
  }

}

我将如何替换对

的调用
JE.events.model.get(..);

类似

self.model.get(..);

整个代码或多或少都在这个要点https://gist.github.com/966270 中。这个想法是在 js 中创建一个非常简单的 MVC(我的第一次尝试),我可以轻松地重用它。欢迎改进!

【问题讨论】:

    标签: oop javascript-framework javascriptmvc


    【解决方案1】:
    JE.events = (function {
      // Create closure
    
      // Declare controller and model as local
      var Controller = {
        init: function(){            
            $(".monthheader").click(function () { 
                Model.get($(this).attr('title')); 
                return false;
            });
    
            return this;
        }
      }
    
      var Model = {
        get: function(monthnum){
            ...
        }
      }
    
      // return object thats assigned to JE.events
      return {
        controller: Controller,
        model: Model
      }
    
    )();
    

    您可能还想查看backbonespine,它们是轻量级 MVC 框架。

    它们为您提供了一些简单的抽象和大量控制。还有小而简单的。

    如果我要从头开始编写一个微型 MVC 框架,它将收敛到主干或主干,因此使用这两者之一可能会更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-26
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2012-07-02
      • 1970-01-01
      • 2012-07-24
      相关资源
      最近更新 更多