html:
<div class="ui-grid-a" ng-controller="ChatController">
<div class="ui-block-a" id="incomingMessages">
<div class="message" ng-repeat="message in messages">
<span class="time">{{ message.time | date:'hh:mm'}}</span>
<span class="username">{{message.username}} </span>
{{message.text}}
</div>
</div>
<div class="ui-block-a" id="form">
<div data-role="fieldcontain">
<form ng-submit="addMessage()" data-ajax="false">
<input ng-model="yourMessageText" id="messageText"
placeholder="Write your message" />
</form>
</div>
</div>
</div>
js:
function ChatController($scope) {
$scope.messages = [{text: 'test1', username: 'un', time: new Date()}]; // are assembled from text and username and time
// this one gets updated in browser
$scope.messages.push({text: 'test2', username: 'un', time: new Date()});
$scope.subscribeToChatChannel = function(uuid) {
pubnub.subscribe({
channel : uuid,
callback : function(message) {
//appendMessageToDom(message);
//setChatBuddy(message.uuid, message.un);
// this one doesn't update in browser, but if I inspect $scope.messages in browser console,
// message is pushed into array
$scope.messages.push(message);
console.log(message);
},
connect : function() {
console.log("CHAT CONNECTION ESTABLISHED.");
},
error : function(e) {
console.log(e);
}
});
}
$scope.subscribeToChatChannel('fakeUuid');
// working fine
$scope.addMessage = function() {
var myUsername = localStorage.getItem('username');
$scope.messages.push({
text: $scope.yourMessageText,
username: myUsername,
time: new Date()
});
$scope.yourMessageText = '';
//sendMessageToChaBuddy(chatBuddy.uuid);
sendMessageToChaBuddy('fakeUuid'); // check this fakeUuid as channel in pubnub console ;)
};
}