cmd模块内部怎么调用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 模板渲染后显示在页面的内容的容器id
      * @param data 服务器返回的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 分页的参数
          * @param templateId 分页需要渲染的模板的id
          * @param resultContentId 模板渲染后显示在页面的内容的容器id
          * @param url 向服务器请求分页的url链接地址
          */
         renderPageData: function(laypageDivId, pageParams, templateId, resultContentId, url) {
             // 判断分页参数是否存在
             // 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("服务器未响应,请稍候再试");
                     } 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
定义的模块,自己怎么调用内部的方法,请问我这段代码应该怎么修正!?
阅读 4.9k
1 个回答

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);
                }
            });
        }
    }
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题