鍍金池/ 問答/Linux  網(wǎng)絡(luò)安全  HTML/ cmd模塊內(nèi)部怎么調(diào)用module.exports里面的方法才是有效的?

cmd模塊內(nèi)部怎么調(diào)用module.exports里面的方法才是有效的?

我有個文件layFactory.js配置如下:
 define(function(require, exports, module) {

     // 前置依賴
     var _tpl = require('mod/private/tplFactory');
     var layer = layui.layer,
         form = layui.form,
         table = layui.table,
         laypage = layui.laypage,
         laydate = layui.laydate,
         laytpl = layui.laytpl;
     /**
      * 分頁模板的渲染方法
      * @param templateId 分頁需要渲染的模板的id
      * @param resultContentId 模板渲染后顯示在頁面的內(nèi)容的容器id
      * @param data 服務(wù)器返回的json對象
      */
     function renderTemplate(templateId, resultContentId, data) {
         laytpl($("#" + templateId).html()).render(data, function(html) {
             // console.log(html);
             $("#" + resultContentId).html(html);
         });
         form.render();
     };

     module.exports = {
         /**
          * layuilaypage 分頁封裝
          * @param laypageDivId 分頁控件Div層的id
          * @param pageParams 分頁的參數(shù)
          * @param templateId 分頁需要渲染的模板的id
          * @param resultContentId 模板渲染后顯示在頁面的內(nèi)容的容器id
          * @param url 向服務(wù)器請求分頁的url鏈接地址
          */
         renderPageData: function(laypageDivId, pageParams, templateId, resultContentId, url) {
             // 判斷分頁參數(shù)是否存在
             // if (isNull(pageParams)) {
             //     pageParams = {
             //         pageIndex: 1,
             //         pageSize: 5
             //     }
             // }
             $.ajax({
                 url: url, //basePath + '/sysMenu/pageSysMenu',
                 method: 'get', //post
                 data: pageParams, //JSON.stringify(datasub)
                 async: false, //true
                 complete: function(XHR, TS) {},
                 error: function(XMLHttpRequest, textStatus, errorThrown) {
                     if ("error" == textStatus) {
                         error("服務(wù)器未響應(yīng),請稍候再試");
                     } else {
                         error("操作失敗,textStatus=" + textStatus);
                     }
                 },
                 success: function(data) {
                     var jsonObj;
                     if ('object' == typeof data) {
                         jsonObj = data;
                     } else {
                         jsonObj = JSON.parse(data);
                     }
                     //  renderTemplate(templateId, resultContentId, jsonObj);

                     _tpl.extCustomRender(jsonObj, templateId, resultContentId);

                     //重新初始化分頁插件
                     laypage.render({
                         elem: laypageDivId,
                         curr: 1, //jsonObj.pager.pageIndex
                         count: 50, //jsonObj.pager.totalPage
                         theme: '#274185',
                         limit: 5,
                         first: '首頁',
                         last: '末頁',
                         // layout: ['prev', 'first', 'page', 'next', 'last'],
                         jump: function(obj, first) { //obj是一個object類型。包括了分頁的所有配置信息。first一個Boolean類,檢測頁面是否初始加載。非常有用,可避免無限刷新。
                             //    pageParams.pageIndex = obj.curr;
                             //    pageParams.pageSize = jsonObj.pager.pageSize;
                             if (!first) {
                                 renderPageData(laypageDivId, pageParams, templateId, resultContentId, url);
                             }
                         }
                     });
                 }
             });
         }
     }
 });

問題代碼段:

 laypage.render({
                         elem: laypageDivId,
                         curr: 1, //jsonObj.pager.pageIndex
                         count: 50, //jsonObj.pager.totalPage
                         theme: '#274185',
                         limit: 5,
                         first: '首頁',
                         last: '末頁',
                         // layout: ['prev', 'first', 'page', 'next', 'last'],
                         jump: function(obj, first) { //obj是一個object類型。包括了分頁的所有配置信息。first一個Boolean類,檢測頁面是否初始加載。非常有用,可避免無限刷新。
                             //    pageParams.pageIndex = obj.curr;
                             //    pageParams.pageSize = jsonObj.pager.pageSize;
                             if (!first) {
                                 renderPageData(laypageDivId, pageParams, templateId, resultContentId, url);
                             }
                         }
  });
報錯提示:

clipboard.png

我的代碼組織方式是用sea.js在維護的,就是像這種用module.exports
定義的模塊,自己怎么調(diào)用內(nèi)部的方法,請問我這段代碼應(yīng)該怎么修正?。?/blockquote>
回答
編輯回答
夏木

module.exports 是一個對象,理論上來說是可以使用 this.xxx 的……因為好久沒用 sea.js,所以需要試驗一下,以下是示意

define(function(require, exports, module) {
    module.exports = {
        renderPageData: function() {
            var _this = this;   // ← 注意這里
            $.ajax({
                success: function(data) {
                    // ↓ 注意這里
                    _this.renderPageData(laypageDivId, pageParams, templateId, resultContentId, url);
                }
            });
        }
    }
});
2017年3月19日 06:28