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を作成することです(私の最初の試み)。これは簡単に再利用できます。改善は大歓迎です!

4

1 に答える 1

0
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
  }

)();

軽量のMVCフレームワークであるバックボーンまたはスパインも確認することをお勧めします。

それらはあなたにいくつかの単純な抽象化と多くの制御を与えます。小さくてシンプルなものもあります。

マイクロMVCフレームワークを最初から作成する場合、バックボーンまたはスパインのいずれかに収束するため、これら2つのうちの1つを使用する方がよい場合があります。

于 2011-05-11T11:39:37.973 に答える