Ajax with CI Restful API

$(document).ready(function(){
    form_funs.init();
});

var form_funs = {
    container: function(){
        return $form = $('#iform');
    }, 
    init: function(){
        var wrap = this;
        var $form = wrap.container();

        $('#group').on('change', function(){
            wrap.get();
        });

        $('#bt_update').on('click', function(e){
            if($form.valid() == true){
                wrap.update();
            }
        });

        $('#bt_new').on('click', function(e){
            if($form.valid() == true){
                wrap.new();
            }
        });

        $('#bt_delete').on('click', function(e){
            if(confirm('Are you sure to delete this item?')){
                wrap.delete();
            }
        });
    }, 
    params: function(){
        var wrap = this;
        var $form = wrap.container();

        var params = {
            group: $form.find('[name="group"]').val(), 
            name: $form.find('[name="name"]').val(), 
            description: $form.find('[name="description"]').val(), 
            permissions: wrap.checked_values('permissions')
        }

        return params;
    }, 
    checked_values: function(field_name){
        var arr = [];
        //console.log($('#iform').find('[name="' + field_name + '"]:checked').size());
        $('#iform').find('[name="' + field_name + '"]:checked').each(function(index){
            var $this = $(this);
            arr.push($this.val());
        });

        return arr;
    }, 
    refresh: function(){
        var wrap = this;

        wrap.clear();

        wrap.get_groups();
    }, 
    clear: function(){
        var wrap = this;
        var $form = wrap.container();

        $form.find('[name="group"]').val('');
        $form.find('[name="name"]').val('');
        $form.find('[name="description"]').val('');
        $form.find('[name="permissions"]').prop('checked', false);
    }, 
    get_groups: function(callback){
        var wrap = this;

        $.get(base_url + 'api/users/groups/', {}, function(data){
            console.log(data);
            if(typeof data.status == 'undefined'){
                alert(data);
            }else if(data.status == 'fail'){
                alert(data.message);
            }else if(data.status == 'success'){
                if(typeof callback == 'function'){
                    callback.call(null, data.response);
                }else{
                    wrap.update_groups(data.response);
                }
            }
        });
    }, 
    update_groups: function(data){
        var wrap = this;
        var $select = $('#group');
        $select.find('option:gt(0)').remove();
        for(var i = 0; i < data.length; i++){
            var item = data[i];
            $select.append('<option value="' + item.id + '">' + item.name + '</option>');
        }
    }, 
    fill: function(data){
        var wrap = this;
        var $form = wrap.container();

        $form.find('[name="group"]').val(data.id);
        $form.find('[name="name"]').val(data.name);
        $form.find('[name="description"]').val(data.description);

        var permissions = data.permissions;
        if(typeof permissions != 'undefined' && permissions != null){
            permissions = $.parseJSON(permissions);

            $form.find('[name="permissions"]').each(function(index){
                var $this = $(this);
                //console.log($.inArray($this.val(), permissions));
                if($.inArray($this.val(), permissions) != -1){
                    $this.prop('checked', true);
                }
            });
        }
    }, 
    get: function(callback){
        var wrap = this;
        var params = wrap.params();

        $.get(base_url + 'api/users/group/', params, function(data){
            //console.log(data);
            if(typeof data.status == 'undefined'){
                alert(data);
            }else if(data.status == 'fail'){
                alert(data.message);
            }else if(data.status == 'success'){
                if(typeof callback == 'function'){
                    callback.call(null, data.response);
                }else{
                    wrap.fill(data.response);
                }
            }
        });
    }, 
    new: function(callback){
        var wrap = this;
        var $form = wrap.container();
        
        var params = wrap.params();
        $.post(base_url + 'api/users/new_group/', params, function(data){
            console.log(data);
            if(typeof data.status == 'undefined'){
                alert(data);
            }else if(data.status == 'fail'){
                alert(data.message);
            }else if(data.status == 'success'){
                if(typeof callback == 'function'){
                    callback.call(null);
                }else{
                    wrap.refresh();
                }
            }
        });
    }, 
    update: function(callback){
        var wrap = this;
        var $form = wrap.container();
        
        var params = wrap.params();
        $.post(base_url + 'api/users/group/', params, function(data){
            console.log(data);
            if(typeof data.status == 'undefined'){
                alert(data);
            }else if(data.status == 'fail'){
                alert(data.message);
            }else if(data.status == 'success'){
                if(typeof callback == 'function'){
                    callback.call(null);
                }else{
                    wrap.refresh();
                }
            }
        });
    }, 
    delete: function(callback){
        var wrap = this;
        var $form = wrap.container();
        
        var params = wrap.params();
        $.post(base_url + 'api/users/delete_group/', params, function(data){
            console.log(data);
            if(typeof data.status == 'undefined'){
                alert(data);
            }else if(data.status == 'fail'){
                alert(data.message);
            }else if(data.status == 'success'){
                if(typeof callback == 'function'){
                    callback.call(null);
                }else{
                    wrap.refresh();
                }
            }
        });
    }
}