samedi 31 janvier 2015

Cannot access proper firebase item key to pass to delete function


Having a sticky issue with firebase. I am trying to get access to the long format ID which firebase auto-generates for each collection item (eg. -JgnIsMlTLaPuDMEtQP2) to pass to my delete function. I previously got it to work in a different angular app as follows:


HTML



<tbody ng-repeat="(key,employee) in employees">
<tr>
<td>{{employee.employeeName}}</td>
<td>{{employee.employeeAge}}</td>
<td>{{key}}</td> // the key param appeared perfectly in this case
<td>
<button class="btn btn-danger"
ng-click="deleteEmployee(key, employee.employeeName)">
delete <span class="glyphicon glyphicon-remove"></span>
</button>
</td>
</tr>
</tbody>


Javascript



$scope.deleteEmployee = function(key,name) {
// get ref to db collectionvar employeeRef = new Firebase("http://ift.tt/1yZ2hOj");
// append item key to the path and run remove method
var newRef = employeeRef.child(key);
newRef.remove();
// confirm delete
alert(name + " has been deleted");
}


The above code receives the correctly formatted key and deletes the item perfectly - which is immediately reflected in the view.


However, I'm trying it with a different app and the key value is being set to an incremental index (eg. 0,1,2,3,4...) and so it doesn't work when passed to the delete function. Here is the new code.


HTML



<div class="row">
<ul id="messages" ng-show="messages.length">
<li ng-repeat="(key, message) in messages | reverse">{{message.text}}
<small>{{key}}</small> // WRONG VALUE DISPLAYED HERE
<button class="btn btn-xs btn-danger" ng-click="delMessage(key)" label="Remove">Delete</button>
</li>
</ul>
</div>


Javascript



//delete message
$scope.delMessage = function(messageKey) {
var messageRef = new Firebase('http://ift.tt/1A9nTJy');
// append item key to the path and run remove method
var messageRef = messageRef.child(messageKey);
if( messageRef ) {
messageRef.remove();
console.log("DEL: " + messageRef); // this logs the endpoint with index number
console.log("Message removed successfully!");
} else {
console.log("uh oh...problem!");
}
};


The format of messageRef when logged to console is



http://ift.tt/15RCfRQ


but instead should be



http://ift.tt/1A9nTJA


I have tried every combination that I could find to get it to work but nothing fixes it so far. I'm not sure why it would work in the first piece of code and not the second. The only thing I can add is the app that isn't working was created via firebase-tools in the terminal whereas the working app is just some angular code with firebase bolted on. I have also tried changing remove() to $remove but that throws undefined method errors. I'm totally stumped. Any assistance is appreciated.





Aucun commentaire:

Enregistrer un commentaire