0
votes

What I'm trying to do: Update the status to "TAKEN" when the chat is closed.

Issue: Can't get $scope.currentChat.$set() or $scope.currentChat.$update() to work when trying to update the status. (See the $scope.close() function.)

What I've tried: Various methods including $set, $update; I don't know. A lot of things. Been researching this for several hours, and can't find a solution that works.

NOTES:

$scope.currentChat.$set({status:"TAKEN"}); Doesn't work. $scope.currentChat.$getRecord('status'); Works. Returns:

Object {$value: "OPEN", $id: "status", $priority: null}

So what exactly is going on here? Why can't I seem to set the status to TAKEN?

The issue is currently in the $scope.close() function, when trying to update the status:

// $SCOPE.CLOSE
// - Closes the current ticket.
$scope.close = function() {
    // $scope.ticketObject.status = "TAKEN";            
    // $scope.currentChat.$set({status:"TAKEN"});
    console.log("===========================");
    console.log("STATUS:");
    console.log($scope.currentChat.$getRecord('status'));
    console.log($scope.currentChat['status']);
    console.log("===========================");
    $scope.ticketObject = {};
    $scope.ticket = false;
    $scope.toggle();
}

Here's my code:

bloop.controller('HomeCtrl', ['$scope', '$firebase', function($scope, $firebase) {

    console.log("HomeController!");
    var url = 'https://**********.firebaseio.com/tickets/';
    var ref = new Firebase(url);

    // $SCOPE.CREATETICKET
    // - This function makes a connection to Firebase and creates the ticket.
    $scope.createTicket = function() {
        $scope.tickets = $firebase(ref).$asArray();
        $scope.tickets.$add($scope.ticketObject).then(function(r) {
            var id = r.name();
            $scope.currentFBID = id;
            $scope.syncTickets();
            console.log("===========================");
            console.log("CREATED TICKET: " + $scope.currentFBID);
            console.log("URL: " + url + $scope.currentFBID);
            console.log("===========================");
        });
    }

    // $SCOPE.SYNCTICKETS
    // - This function makes a connection to Firebase and syncs the ticket with the $scope to easily update the tickets.
    $scope.syncTickets = function() {
        var ticketRefURL = new Firebase(url + $scope.currentFBID);
        $scope.currentChat = $firebase(ticketRefURL).$asArray();
        $scope.currentChat.$save($scope.ticketObject);

        var archiveRefURL = new Firebase(url + $scope.currentFBID + "/archive");
        $scope.currentChat.archive = $firebase(archiveRefURL).$asArray();

        console.log("===========================");
        console.log("SAVED TICKET: " + $scope.currentFBID);
        console.log("URL: " + ticketRefURL);
        console.log("ARCHIVE URL: " + archiveRefURL);
        console.log("===========================");
    }

    // $SCOPE.POST
    // - This function pushes whatever is typed into the chat into the chat archive.
    // - $scope.ticketObject.archive (is an array of objects)
    $scope.post = function(name) {
        // Push to ticketObject.archive array...
        $scope.ticketObject.archive.push({
            "name" : name,
            "text" : $scope.chatText
        });

        // Logging the array to make sure it exists...
        console.log("===========================");
        console.log("CHAT ARCHIVE:");
        console.log($scope.ticketObject.archive);
        console.log("===========================");

        $scope.currentChat.archive.$add({
            "name" : name,
            "text" : $scope.chatText
        });

        // This resets the text area so it's empty...
        $scope.chatText = "";
    } // WORKS

    // $SCOPE.CLOSE
    // - Closes the current ticket.
    $scope.close = function() {
        // $scope.ticketObject.status = "TAKEN";            
        // $scope.currentChat.$set({status:"TAKEN"});
        console.log("===========================");
        console.log("STATUS:");
        console.log($scope.currentChat.$getRecord('status'));
        console.log($scope.currentChat['status']);
        console.log("===========================");
        $scope.ticketObject = {};
        $scope.ticket = false;
        $scope.toggle();
    }

    // $SCOPE.TOGGLE
    // - This function toggles the chat to be either open or closed.
    $scope.toggle = function() {
        if($scope.toggleState === false) {
            $scope.toggleState = true;
            $scope.checkTicket();
        } else if($scope.toggleState === true) {
            $scope.toggleState = false;
        }
    }

    // $SCOPE.CHECKTICKET
    // - This function checks to see if there's an existing ticket.
    // - If there's not an existing ticket, it creates one.
    $scope.checkTicket = function() {
        if($scope.ticket === false) {
            // Generate New Ticket Data
            $scope.ticketObject = newTicket();

            // Create the Ticket
            $scope.createTicket();

            // Ticket now exists.
            $scope.ticket = true;

        }
    }

    function newTicket() {
        var ticketID = generateTicketID();

        var newTicket = {
            id: ticketID,
            status: "OPEN",
            name: "N/A",
            email: "N/A",
            date: generateDate(),
            opID: "Unassigned",
            opName: "Unassigned",
            archive: [],
            notes: []
        }

        return newTicket;
    }

    function generateTicketID() {
        var chars = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
        var result = '';
        for(var i=12; i>0; --i) {
            result += chars[Math.round(Math.random() * (chars.length - 1))];
        }
        return result;
    }

    function generateDate() {
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth() + 1;
        var yyyy = today.getFullYear();

        if(dd < 10) {
            dd = '0' + dd;
        }

        if(mm < 10) {
            dd = '0' + mm;
        }

        var date = mm + "/" + dd + "/" + yyyy;
        return date;
    }
}]);
1
Can you reduce the code to show just a single instance of the problem that you're asking about? - Frank van Puffelen
The issue lies in the $scope.close() function. I added a code block. It's the first one. I pasted the rest, because there's several other functions, and it's there if someone needs to see something. Thanks. - Lindsay

1 Answers

1
votes

$update and $set are part of the $firebase API. You are attempting to call them on the synchronized array returned by $asArray(), which is a $FirebaseArray instance. That has its own API, which includes neither update nor set.