可以$ $更新或$ set~" undefined不是函数" ~AngularFire

时间:2014-11-12 11:50:04

标签: angularjs firebase angularfire

我正在尝试做什么: 聊天结束后,将状态更新为“TAKEN”。

问题: 尝试更新状态时无法使$scope.currentChat.$set()$scope.currentChat.$update()生效。 (参见$scope.close()功能。)

我尝试了什么: 各种方法,包括$ set,$ update;我不知道。很多东西。正在研究这几个小时,并找不到有效的解决方案。

备注:

$scope.currentChat.$set({status:"TAKEN"});不起作用。 $scope.currentChat.$getRecord('status');有效。返回:

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

那到底是怎么回事?为什么我似乎无法将状态设置为TAKEN?

当尝试更新状态时,问题当前在$ scope.close()函数中:

// $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();
}

这是我的代码:

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 个答案:

答案 0 :(得分:1)

$ update和$ set是$firebase API的一部分。您试图在$asArray()返回的同步数组上调用它们,$FirebaseArray实例是{{1}}实例。那有its own API,既不包括更新也不包括设置。