javascript oop 和这个

发布于 2024-11-06 06:28:57 字数 661 浏览 6 评论 0原文

给出以下代码:

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(我的第一次尝试),我可以轻松地重用它。欢迎改进!

Given the following code:

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){
        ...
    }
  }

}

How would i replace the call to

JE.events.model.get(..);

by something like

self.model.get(..);

The whole code is more or less in this gist https://gist.github.com/966270 . The idea is to create a really simple MVC in js (my first attempt) that i can reuse easily. Improvements are welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2024-11-13 06:28:57
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 框架,它将收敛到主干或骨干,因此最好使用这两者之一。

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
  }

)();

You may also want to look at backbone or spine which are lightweight MVC frameworks.

They give you some simple abstractions and a lot of control. There also small and simple.

If I were to write a micro MVC framework from scratch it would converge to either backbone or spine so it might be better to use one of those two.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文