review.js

define(["../../javascripts/app/common"],
    function(common) {
        //return a function to define "foo/title".
        //It gets or sets the window title.
        return{
    // Add Venue
    getReviewObject:function(type){

        var nReview = {
            'comment': $('#reviewUserComment').val(),
            'curdate': $('#reviewCommentTime').val(),
            'venue_id': $('#reviewVenueId').val(),
            'userName': $('#reviewUserName').val(),
            'userPic': $('#reviewUserPic').val(),
            'userEmail': $('#reviewUserEmail').val(),
            'reviewed': $('#reviewUserReviewed').val()
            }
            nReview._id= $('#reviewUserName').data('id');

       return nReview;
    },
    editReview:function(event){
       event.preventDefault();
       // If it is, compile all user info into one object
        var nReview = event.data.self.getReviewObject('info');

        // Use AJAX to post the object to our adduser service
        $.ajax({
            type: 'PUT',
            data: nReview,
            url: '/admin/comment/edit/1231',
            dataType: 'JSON'
        }).done(function( response ) {
            alert('success');
            // Check for successful (blank) response
            if (typeof response  ==="object") {
                // Clear the form inputs
                $("[id^=review]").val('');
                $("[id*=Sel]").val('0');
                // Update the table
                event.data.self.reviewListData = response;
                event.data.self.populateTable();
            }
            else {
                // If something goes wrong, alert the error message that our service returned
                alert('Error: ' + response.msg);
            } 
        });
    },
        init:function(){
            var self = this;
            $.getJSON( '/admin/getcomment', function( data ) {
                self.reviewListData = data;
                self.populateTable();
            });
        },

        initialised:false,
        reviewListData:[],
        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.reviewListData, function(){
            tableContent += '<tr>';
            tableContent += '<td><a href="#" class="linkshowreview" rel="' + this._id + '" title="Show Details">'
            + this.userEmail + '</td>';
            tableContent += '<td>' + this.venue_id + '</td>';
            tableContent += '<td>' + this.userName + '</td>';
            tableContent += '<td><a href="#" class="linkdeletereview" data-url="/admin/comment/delete" rel="' + this._id + '">delete</a></td>';
            tableContent += '</tr>';
        });

        // Inject the whole content string into our existing HTML table
        $('#reviewList table tbody').html(tableContent);
        if(!this.initialised){
            $('#edit').on('click', {'self':this},this.editReview);
            $('#reviewList table tbody').on('click', 'td a.linkdeletereview', {'url':'/admin/comment/delete/','type':'review'},common.deleteEntity);
            $('#reviewList table tbody').on('click', 'td a.linkshowreview',{'self':this}, this.showInfo);
            this.initialised = true;
        }

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

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

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

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

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

        //Populate Info Box
        $('#reviewUserName').val(thisObject.userName);
        $('#reviewUserName').data('id',thisObject._id);
        $('#reviewUserEmail').val(thisObject.userEmail);
        $('#reviewUserComment').val(thisObject.comment);
        $('#reviewVenueId').val(thisObject.venue_id);
        $('#reviewCommentTime').val(thisObject.curdate);
        $('#reviewUserPic').val(thisObject.userPic);
        $('#reviewUserReviewed').val(thisObject.reviewed);
    }

}
});

Leave a Reply

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