通过Context让JavaScript写的有层次感 返回

精华
2 2057
该叫什么 小杰 发布于2017/3/8
悬赏:5 飞吻
/// <reference path="../../Framework/jquery.min.js" />
var Selectors = {
    Info: {
        BtnSubmit:"#btnSubmit"
    },
    List: {
        BtnSearch: "#btnSearch",
        BtnEdit: "#btnModifynEdit",
        BtnAdd: "#btnAdd",
        BtnDelete: "#btnDisable"
    }
};
var CalendarTypePage = {
    onInit: function () { //页面初始化
        var context = this;
        context.load.setRootContext(context);
        context.load.start();
        context.onPageEvent();
    },
    onPageEvent: function () {
        var context = this;
        context.bind.setRootContext(context);
        context.bind.start();
    },
    load: {
        start: function () {
            this.grid();
        },
        setRootContext: function (context) { this.context = context; },
        grid: function () {

        }
    },
    //事件绑定
    bind: {
        start: function () {
            this.search();
            this.add();
            this.del();
            this.edit();
        },
        setRootContext: function (context) { this.context = context; },
        //list
        search: function () {

        },
        add: function () {

        },
        del: function () {

        },
        edit: function () {

        },
        //Info
        submit: function () {
            var context = this.context;
            $(Selectors.Info.BtnSubmit).click(function () {
                var isOk = context.tools.validates();
                alert(isOk);
            });
        }
    },
    //常用工具
    tools: {
        validates: function () {
            return Pms.Validate.validate();
        }
    }
}
$(function () {
    var page = CalendarTypePage.onInit();
})


热忱回答2

  • 小杰 小杰 VIP0
    2017/3/10
    //通用页面对象
    /**********通用页面对象开始**********/
    var CommonPageSelectors = {};
    var CommonPage = {
        onInit: function (Selectors) { //页面初始化
            CommonPageSelectors = Selectors;
            var context = this;
            context.load.setRootContext(context);
            context.load.start();
            context.onPageEvent();
        },
        onPageEvent: function () {
            var context = this;
            context.bind.setRootContext(context);
            context.bind.start();
        },
        load: {
            start: function () {
                this.grid();
                this.select();
                if (this.callBack != null) {
                    this.callBack();
                }
            },
            callBack: null,//外部接口赋值可以添加到load.star执行
            setRootContext: function (context) { this.context = context; },
            grid: function () {
                var GridContext = {
                    create: function () {
                        var gridContext = this;
                        $(CommonPageSelectors.List.Grid).bootstrapTable({
                            url: ActionUrls.Grid,
                            method: 'get', //请求方式
                            cache: false, //URL禁止缓存
                            //height: $(window).height() - 200,//行高,如果没有设置height属性,表格自动根据记录条数觉得表格高度
                            striped: true, //是否显示行间隔色
                            pagination: true, //启动分页
                            sidePagination: "server", //服务器端分页
                            pageNumber: 1, //当前页码
                            pageSize: JsGridDefaultOptions.PageSize, //每页的记录行数(*)
                            pageList: JsGridDefaultOptions.PageList, //可供选择的每页的行数(*)
                            minheight: JsGridDefaultOptions.PageHeight.call($(CommonPageSelectors.List.Grid)), //行高 如果没有自适应
                            sortable: true, //启动排序
                            sortOrder: Settings.GridOrderType,
                            sortName: Settings.GridDefaultSort,
                            search: false, //启动搜索
                            showColumns: true, //显示筛选列
                            showRefresh: true,
                            clickToSelect: true, //是否启用点击选中行
                            refresh: { silent: true },
                            queryParamsType: '', // //设置为undefined可以获取pageNumber,pageSize,searchText,sortName,sortOrder;设置为limit可以获取limit, offset, search, sort, order
                            queryParams: function (params) {
                                return gridContext.queryParams(params);
                            },
                            onDblClickRow: function (row, $element) {
                                $(CommonPageSelectors.List.Grid).bootstrapTable('uncheckAll');
                                $(CommonPageSelectors.List.Grid).bootstrapTable('check', $($element).data("index"));
                                $(CommonPageSelectors.List.BtnEdit).click();
                            },
                            onClickRow: function (row, $element, field) {
    
                            },
                            onPageChange: function (size, number) {
                                //分页事件
                            },
                            onSort: function (name, order) {
                                //排序事件
                            },
                            formatNoMatches: function () {
                                //显示没有匹配的文字
                                return $JsLang.Common.NoRecord;
                            }
                        });
                    },
                    queryParams: function (params) {
    
                        var temp = {   //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的
                            PageSize: params.limit,   //页面大小
                            PageIndex: params.pageNumber - 1  //页码
                        };
    
                        //动态添加参数
                        $(CommonPageSelectors.List.SearchBar).find('input[name],select[name]').each(function () {
                            temp[$(this).attr('name')] = $(this).val();
                        });
                        //处理排序字段别名
                        if (params.sortName != null) {
                            var sortObj = $(CommonPageSelectors.List.GridParamBox).find("[name=" + params.sortName + "]");
                            if (sortObj.size() > 0) {
                                temp.SortField = sortObj.val();
                            }
                        }
                        if (params.sortOrder != null) {
                            temp.SortType = params.sortOrder != "desc";
                        }
                        return temp;
    
                    },
                    refresh: function () {
                        $(CommonPageSelectors.List.Grid).bootstrapTable('refresh');
                    },
                    getSelectIds: function () {
                        var ids = new Array();
                        var elements = $(CommonPageSelectors.List.Grid).bootstrapTable('getSelections');
                        if (elements == null || elements.length <= 0) {
                            $.pms.msg(JsResources.J0000000003);
                            return;
                        }
                        $(elements).each(function (i, v) {
                            ids.push(v[Settings.PrimaryKey]);
                        });
                        return ids;
                    }
                }
                GridContext.create();
                this.gridContext = GridContext;
            },
            select: function () {
                $("select").selectpicker();
            }
        },
        //事件绑定
        bind: {
            start: function () {
                this.search();
                this.add();
                this.del();
                this.edit();
                this.cancel();
                this.submit();
                if (this.callBack != null) {
                    this.callBack();
                }
            },
            callBack: null,//外部接口赋值可以添加到bind.star执行
            setRootContext: function (context) { this.context = context; },
            //list
            search: function () {
                var context = this.context;
                var gridContext = context.load.gridContext;
                $(CommonPageSelectors.List.BtnSearch).click(function () {
                    gridContext.refresh();
                });
            },
            add: function () {
                var context = this.context;
                $(CommonPageSelectors.List.BtnAdd).click(function () {
                    $(CommonPageSelectors.List.Dialog).data("isAdd", true);
                    $.pms.openHtml(CommonPageSelectors.List.Dialog, JsResources.AddTitle, Settings.windowWidth, Settings.windowHeight);
                    context.tools.clearForm();
                });
            },
            del: function () {
                var context = this.context;
                var gridContext = context.load.gridContext;
                $(CommonPageSelectors.List.BtnDelete).click(function () {
                    $.pms.confirm(JsResources.IsDelete, function () {
                        var ids = gridContext.getSelectIds();
                        if (ids == null || ids.length == 0) {
                            $.pms.msg(JsResources.NoRecord);
                            return;
                        }
                        context.tools.ajax(ActionUrls.Delete, { ids: ids }, function (msg) {
                            gridContext.refresh();
                        });
                        $.pms.closeAll();
                    });
                });
            },
            edit: function () {
                var context = this.context;
                var loadContext = this;
                var gridContext = context.load.gridContext;
                $(CommonPageSelectors.List.BtnEdit).click(function () {
                    var ids = gridContext.getSelectIds();
                    if (ids == null || ids.length == 0) {
                        $.pms.msg(JsResources.NoRecord); return;
                    }
                    $(CommonPageSelectors.List.Dialog).data("isAdd", false);
                    $.pms.openHtml(CommonPageSelectors.List.Dialog, JsResources.EditTitle, Settings.windowWidth, Settings.windowHeight);
                    context.tools.clearForm();
                    context.tools.ajax(ActionUrls.GetId, { id: ids[0] }, function (msg) {
                        context.tools.fillForm(msg.Data);
                        $(CommonPageSelectors.Info.TxtWeek).mulitiDay("refresh");
                        $(CommonPageSelectors.Info.TxtDay).mulitiDay("refresh");
                        if (loadContext.editCallBack != null) {
                            loadContext.editCallBack(context);
                        }
                    });
                });
            },
            //Info
            submit: function () {
                var context = this.context;
                var loadContext = this;
                var gridContext = context.load.gridContext;
                $(CommonPageSelectors.Info.BtnSubmit).click(function () {
                    var isOk = context.tools.validates();
                    if (isOk) {
                        var isAdd = $(CommonPageSelectors.List.Dialog).data("isAdd");
                        context.tools.ajaxSubmit(
                            $(CommonPageSelectors.List.Dialog).find("form"),
                            isAdd ? ActionUrls.Add : ActionUrls.Update,
                            function (msg) {
                                $.pms.close(CommonPageSelectors.List.Dialog);
                                $.pms.msg(isAdd ? JsResources.AddSuccess : JsResources.EditSuccess);
                                gridContext.refresh();
                                if (loadContext.submitCallBack != null) {
                                    loadContext.submitCallBack(context);
                                }
                            });
    
                    }
                });
            },
            cancel: function () {
                var context = this.context;
                var gridContext = context.load.gridContext.refresh();
                $(CommonPageSelectors.Info.BtnCancel).click(function () {
                    $.pms.close(CommonPageSelectors.List.Dialog);
                });
            }
        },
        //常用工具
        tools: {
            validates: function () {
                return Pms.Validate.validate();
            },
            clearForm: function () {
                $(CommonPageSelectors.List.Dialog).find("input,select,textarea").not(":radio").val("");
                $(CommonPageSelectors.List.Dialog).find("select").selectpicker("refresh");
            },
            refreshSelect: function () {
                $("select").selectpicker("refresh");
            },
            ajaxSubmit: function (formObj, url, yesAction) {//表单提交
                $.ajax({
                    url: url,
                    data: formObj.serialize(),
                    cache: false,
                    type: "post",
                    traditional: true,
                    success: function (msg) {
                        if (msg.Success == true) {
                            if (yesAction != null) {
                                yesAction(msg);
                            }
                        } else {
                            $.pms.msg(msg.ResultMsg);
                        }
                        $.pms.closeLoading();
                    },
                    error: function (msg) {
                        console.log(msg);
                    }
                });
            },
            ajax: function (url, data, yesAction) {//异步请求支持数组
                $.ajax({
                    url: url,
                    type: "GET",
                    cache: false,
                    data: data,
                    traditional: true,
                    success: function (msg) {
                        if (yesAction == null) {
    
                        } else {
                            yesAction(msg);
                        }
                    },
                    error: function (msg) {
                        console.log(msg);
                    }
                });
            },
            fillForm: function (msg) { //动态填充表单
                for (var key in msg) {
                    var inputObj = $(CommonPageSelectors.List.Dialog).find("[name='" + key + "']");
                    var value = msg[key];
                    if (inputObj.is(":radio")) {
                        inputObj.prop("checked", false);
                        inputObj.first().prop("checked", true);
                        if (value != null && value != "") {
                            inputObj.filter(function () { return $(this).val() == value }).click();
                        }
                    } else {
                        inputObj.val(value);
                    }
                }
                this.refreshSelect();
            },
            bindOrgTree: function (selector, saveId, data, url) { //组定组织树
                this.ajax(url, data, function (msg) {
                    var data = $.parseJSON(msg);
                    //绑定组织 界面查询
                    $(selector).dropDownTreeEasy({
                        dataSource: data,
                        valueControlID: $(saveId)[0].id,
                        click: function (selectObject) {
                            if (selectObject.length > 0) {
                                nodeName = selectObject[0].name;
                            }
                        }
                    });
                });
            }
        }
    }
    /**********通用页面对对象结束**********/


    0 回复
  • 这个是啥子框架的东西哦

    0 回复

版块

学习文档

最新会员

发布达人

回贴达人