subList.js

define(["../../javascripts/app/common"],
    function(common) {
        //return a function to define "foo/title".
        //It gets or sets the window title.
        return{
    // Add subList
    getsubListObject:function(ele){
        var coord =[];
        var prependId ;

        if(ele ==="info"){
            prependId = '#subListInfo';
			var newVersion = parseInt($(prependId + 'Version').val());
			$(prependId + 'Version').val(newVersion);
        }else{
            prependId = '#subList';
        }

        var latC = ($(prependId + 'Latitude').val()==='')?"-1":($(prependId + 'Latitude').val());
        var longC =   ($(prependId + 'Longitude').val()==='')?"-1":($(prependId + 'Longitude').val());
        coord.push(longC);
        coord.push(latC);
        var nsubList = {
            'name': $(prependId + 'Name').val(),
            'image':$(prependId + 'Image').val(),
            'loc':{
                    'coordinates':coord
                },
            'address': $(prependId + 'Address').val(),
            'phone': $(prependId + 'Phone').val(),
            'sDescription': $(prependId + 'ShortDesc').val(),
            'bDescription': $(prependId + 'LongDesc').val(),
            'timings': $(prependId + 'Timings').val(),
            'city':$(prependId + 'SelCity').val(),
            'type':$(prependId + 'SelType').val(),
			'extra1':$(prependId + 'Extra1').val(),
			'extra2':$(prependId + 'Extra2').val(),
			'extra3':$(prependId + 'Extra3').val(),
			'extra4':$(prependId + 'Extra4').val(),
			'extra5':$(prependId + 'Extra5').val(),
			'active':$(prependId + 'SelActive').val(),
			'version':$(prependId + 'Version').val()
                    }

            if(ele ==="info"){
                nsubList._id=$(prependId + 'Name').data('id');
            }


        if(nsubList.name ==='' || nsubList.address ==='' || nsubList.phone ===''
        || nsubList.sDescription ===''
         || nsubList.timings ==='' ||  nsubList.latitude === ''|| nsubList.longitude === '' || nsubList.city ==='' || nsubList.type ==='' ){

             return "empty";
        }
        return nsubList;
    },
    editsubList:function(event){
       event.preventDefault();
       // If it is, compile all user info into one object
        var nsubList = event.data.self.getsubListObject('info');
        if(typeof(nsubList)=='string'){
            return alert('Please fill in all details');
        }
        // Use AJAX to post the object to our adduser service
        $.ajax({
            type: 'PUT',
            data: nsubList,
            url: '/admin/subList/edit/1231',
            dataType: 'JSON'
        }).done(function( response ) {

            alert('success');

            // Check for successful (blank) response
            if (typeof response ==="object") {
                // Clear the form inputs
                $("[id^=subList]").val('');
				$("[id*=Sel]").val('0');
				//$("[id*=SelActive]").val('Y');
                 event.data.self.subListListData = response;
                // Update the table
                // event.data.self.populateTable();

            }
            else {

                // If something goes wrong, alert the error message that our service returned
                alert('Error: ' + response.msg);

            }
        });
    },
    addsubList:function (event) {
        event.preventDefault();
        var nsubList = event.data.self.getsubListObject('');
        if(typeof(nsubList)=='string'){
                    return alert('Please fill in all details');
        }
        // Use AJAX to post the object to our adduser service
        $.ajax({
            type: 'POST',
            data: nsubList,
            url: '/admin/subList/add',
            dataType: 'JSON'
        }).done(function( response ) {

            alert('success');

            // Check for successful (blank) response
            if (typeof response ==="object") {
                // Clear the form inputs
                $("[id^=subList]").val('');
				$("[id*=Sel]").val('0');
				$("[id*=SelActive]").val('Y');
                 event.data.self.subListListData = response;
                // Update the table
                //event.data.self.populateTable();

            }
            else {

                // If something goes wrong, alert the error message that our service returned
                alert('Error: ' + response.msg);

            }
        });
    },
        initialised:false,
        subListListData:[],
        init:function(){

            var self = this;
            self.populateTable();
            /*
            $.getJSON( '/admin/subList', function( data ) {
                self.subListListData = data;
                self.populateTable();
            });
            */
        },
        search:function(event){
            event.preventDefault();
            var self = event.data.self;
            var city = $('#listsubListSelCity').val();
            if (city && city != 0 ) {
                $.getJSON('/admin/subList/search/city/' + city, function( data ) {
                   self.subListListData = data;
                   self.populateTable();
                })
            } else {
                alert('Please select City to get subListing');
            }
        },
    // Fill table with data
    populateTable:function() {

        // Empty content string
        var tableContent = '';

        // For each item in our JSON, add a table row and cells to the content string
        $.each(this.subListListData, function(){
            tableContent += '<tr>';
            tableContent += '<td><a href="#" class="linkshowsubList" rel="' + this._id + '" title="Show Details">'
            + this.name + '</td>';
            tableContent += '<td>' + this.city + '</td>';
            tableContent += '<td>' + this.type + '</td>';
            tableContent += '<td><a href="#" class="linkdeletesubList" data-url="/admin/subList/deletesubList/" rel="' + this._id + '">delete</a></td>';
            tableContent += '</tr>';
        });

        // Inject the whole content string into our existing HTML table
        $('#subListList table tbody').html(tableContent);
        if(!this.initialised){
            $('#btnAddsubList').on('click', {'self':this},this.addsubList);
            $('#edit').on('click', {'self':this},this.editsubList);
            $('#subListList table tbody').on('click', 'td a.linkdeletesubList', {'url':'/admin/subList/delete/','type':'subList'},common.deleteEntity);
            $('#subListList table tbody').on('click', 'td a.linkshowsubList',{'self':this}, this.showInfo);
            $('#btnListsubList').on('click', {'self':this},this.search);
            this.initialised = true;
        }

    },
    // Show User Info
     showInfo:function(event) {

        // Prevent Link from Firing
        event.preventDefault();

        // Retrieve subListname from link rel attribute
        var thissubListName = $(this).attr('rel');

        // Get Index of object based on id value
        var arrayPosition = event.data.self.subListListData.map(function(arrayItem) { return arrayItem._id; }).indexOf(thissubListName);

        // Get our User Object
        var thisObject = event.data.self.subListListData[arrayPosition];

        //Populate Info Box
        $('#subListInfoName').val(thisObject.name);
        $('#subListInfoName').data('id',thisObject._id);
        $('#subListInfoAddress').val(thisObject.address);
        $('#subListInfoPhone').val(thisObject.phone);
        $('#subListInfoLocation').val(thisObject.city);
        $('#subListInfoTimings').val(thisObject.timings);
        $('#subListInfoLatitude').val(thisObject.loc.coordinates[1]);
        $('#subListInfoLongitude').val(thisObject.loc.coordinates[0]);
        $('#subListInfoImage').val(thisObject.image);
        $('#subListInfoShortDesc').val(thisObject.sDescription);
        $('#subListInfoLongDesc').val(thisObject.bDescription);
        $('#subListInfoSelCity').val(thisObject.city);
        $('#subListInfoSelType').val(thisObject.type);
		$('#subListInfoExtra1').val(thisObject.extra1);
        $('#subListInfoExtra2').val(thisObject.extra2);
		$('#subListInfoExtra3').val(thisObject.extra3);
        $('#subListInfoExtra4').val(thisObject.extra4);
		$('#subListInfoExtra5').val(thisObject.extra5);
		$('#subListInfoSelActive').val(thisObject.active);
		$('#subListInfoVersion').val(thisObject.version);

    }

}
});

Leave a Reply

Your email address will not be published. Required fields are marked *