util.js 3.57 KB
var $=require('jquery');
var util={
        __ajax:function(options,callback,notip){
            if(typeof options.data=="string"){
                util.__tip(options.data,"warning");
                return false;
            }else{
                $.ajax({
                    type: 'POST',
                    url: options.url,
                    async: options.async,
                    dataType: 'json',
                    data:options.data||{},
                    success: function(res) {
                        res=res.data;
                        if (("" + res.code) === "200") {
                            notip?callback.bind(this,res)():util.__tip(res.message,"success",callback.bind(this,res));
                            return true;
                        } else {
                            (!notip)&&util.__tip(res.message,"danger");
                            return false;
                        }
                    }
                });
            }
        },
        __ajax2:function(options,callback){
            if(typeof options.data=="string"){
                util.__tip(options.data,"warning");
            }else{
                $.ajax({
                    type: 'POST',
                    url: options.url,
                    async: options.async,
                    dataType: 'json',
                    data:options.data||{},
                    success: function(res) {
                        res=res.data;
                        if (res.code != 200) {
                            util.__tip(res.message,"danger");
                        } else {
                            callback.bind(this,res)();
                        }
                    }
                });
            }
        },
        __tip:function(message,callback){
            //danger
            var options={};
            options.class_name="growl-danger";
            options.title=arguments[0];
            if (arguments.length > 1) {
                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);
                lastargs=lastargs[0];
                if(typeof lastargs==="function"){
                    options.after_open=setTimeout(function() {
                        lastargs&&lastargs();
                    },600); 
                }
            } 
            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 regkey=((+key)==(+key))?"["+key+"]":key;
                                var reg = new RegExp("({" + regkey + "})", "g");
                                result = result.replace(reg, args[1][key]);
                            }
                        }
                    } 
                    var reg = new RegExp("({.*})", "g");
                    result = result.replace(reg, '');
                }
            }
            return result;
        },
        __input:function(id){
            return !!~$.trim($('#'+id).val())?$('#'+id).val():""
        }
    }
module.exports=util;