seajs通过module.constructor.prototype扩展公共方法

看了一遍关于《扩展SeaJS模块定义中的module参数的应用示例》讲得很不错!自己就尝试一下!

http://limu.iteye.com/blog/1136712

https://github.com/seajs/seajs/issues/67

为了能够预加载公共部分模块!使用sea.js提供的preload模块功能!

在安装了官方的项目工具spm后,直接 spm install -g seajs-preload


<html>
  <head>
    <meta charset="utf-8">
    <title>sea.jstitle>
    <script type="text/javascript" src="../../sea-modules/sea.js">script>
    <script type="text/javascript" src="../../sea-modules/seajs-preload.js">script>
  head>
  <body>
    <div id="block">1111div>
  body>
html>
<script type="text/javascript">
seajs.config({
  base : "../../sea-modules/",
  paths : {
    "mPath" : "../../static/module/",
  },
  alias : {
    "public" : "mPath/public"
  },
  preload : [
    "public"
  ]
});
seajs.use("mPath/moduleA");
script>

公共模块部分,模仿了jqery的html方法

define(function(require, exports, module) {

  var mp = module.constructor.prototype;

    mp.mJq = function() {

      var elem;

      (function(args){
        if (typeof args[0] === 'string') {
          elem = document.getElementById(args[0]);
        }
      })(arguments);

      var mJquery = (function(){
        return elem;
      })();

      mJquery.html = function() {
        if (typeof elem == 'object' && typeof arguments[0] != 'undefined') {
          elem.innerHTML = arguments[0];
        } else {
          return elem.innerHTML;
        }
      }

      return mJquery;
    };
});

在其他模块调用

define(function(require, exports, module) {
  var $ = module.mJq;
  console.log($("block").html());
  $("block").html("halo");
});

你可能感兴趣的:(SeaJs)