Skip to content

Instantly share code, notes, and snippets.

@juarezpaf
Forked from taras/pusher_callback
Last active December 24, 2015 01:19
Show Gist options
  • Save juarezpaf/6722956 to your computer and use it in GitHub Desktop.
Save juarezpaf/6722956 to your computer and use it in GitHub Desktop.
App.PostRoute = Em.Route.extend({
model: function(params) {
return Em.$.ajax({
type: 'GET',
url: App.API+'/post/'+ params.post_id,
dataType: 'json',
contentType: 'application/json',
context: this
}).then(function(json) {
if (json.status.code === 200) {//API return status code with success or errors
return App.Post.create(json.data[0]);
} else {
App.notification('Server is failing! Sorry! Try again later.', 'notification-error');
}
});
},
setupController: function(controller, model) {
this.controllerFor('postIndex').set('content', model);
}
});
App.PostsIndexRoute = Em.Route.extend({
model: function() {
return Em.$.ajax({
type: 'GET',
url: App.API+'/posts',
dataType: 'json',
contentType: 'application/json',
context: this
}).then(function(json) {
if (json.status.code === 200) {
var posts = Em.A();
json.data.forEach(function (post){
posts.pushObject(App.Post.create(post));
});
return posts;
} else {
App.notification('Server is failing! Sorry! Try again later.', 'notification-error');
}
});
}
});
var pusher = new Pusher('APP_KEY');
var eventName = 'new-comment';
var callback = function(data) {
var applicationRoute = App.__container__.lookup('route:application');
Em.A(data).map(function(comment){
applicationRoute.findModel( "post", comment.post_id );
post.comments.push(App.Comment.create(comment));
});
};
pusher.bind(eventName, callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment