common.js 2.68 KB
require('../util/jquery.gritter');

var  grid=require('./grid'),
edit = require('./edit'),
dropDown = require('./dropDown'),
dialog=require('./dialog');

var common={
    grid:grid,
    dialog:dialog,
    dropDown:dropDown,
    edit:edit,
    util:{
        __ajax:function(options,callback){
            $.ajax({
                type: 'POST',
                url: options.url,
                dataType: 'json',
                data:options.data||{},
                success: function(res) {
                    res=res.data;
                    if (res.code === 200) {
                        common.util.__tip(res.message,"success",callback);
                    } else {
                        common.util.__tip(res.message,"danger",callback);
                    }
                }
            });
        },
        __tip:function(message,callback){
            if (arguments.length > 1) {
                options.title=arguments[0];
                if (typeof (arguments[1]) === "string") {
                   options.class_name="growl-"+arguments[1];
                }else{
                    options.class_name="growl-success";
                }
                var lastargs = Array.prototype.slice.call(arguments, arguments.length-1);
                if(typeof lastargs==="function"){
                    options.after_open=setTimeout(function() {
                        lastargs&&lastargs();
                    },100) 
                }
            } else {
                console.log("__tip error");
                return false;
            };
            options.sticky=false;
            options.time=1000;

            $.gritter.add(options);
        },
        __template: function () {
            var args = arguments, result;
            if (args.length > 0) {
                if (typeof args[0]==="string") {
                    result = args[0];
                    if (args.length == 2 && typeof args[1]=="object") {
                        for (var key in args[1]) {
                            if (args[1][key] != undefined) {
                                var reg = new RegExp("({" + key + "})", "g");
                                result = result.replace(reg, args[1][key]);
                            }
                        }
                    } else {
                        for (var i = 1; i < args.length; i++) {
                            if (args[i] != undefined) {
                                var reg = new RegExp("({[" + (i - 1) + "]})", "g");
                                result = result.replace(reg, args[i]);
                            }
                        }
                    }
                }
            }
            return result;
        }
    }
}

module.exports=common;