-
-
Save huafu/6565756 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EmBlog = Ember.Application.create({ | |
LOG_TRANSITIONS: true | |
}); | |
EmBlog.Comment = DS.Model.extend({ | |
body: DS.attr('string'), | |
post: DS.belongsTo('post') | |
}); | |
EmBlog.Post = DS.Model.extend({ | |
title: DS.attr('string'), | |
body: DS.attr('string'), | |
comments: DS.hasMany('comment') | |
}); | |
EmBlog.Router.reopen({ | |
location: 'history' | |
// rootURL: '/' | |
}); | |
EmBlog.Router.map(function() { | |
this.resource('posts', function(){ | |
this.route('new'); | |
//routes to /posts/:post_id | |
this.resource('post', {path: ':post_id'}, function(){ | |
this.route('edit'); | |
//routes to /:post_id/comments | |
this.resource('comments', function() { | |
this.route('new'); | |
this.resource('comment', {path: ':comment_id'}, function() { | |
this.route('edit'); | |
}); | |
}); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EmBlog.PostsRoute = Ember.Route.extend({ | |
actions: { | |
removePost: function(post) { | |
//deletes record from ember-data store | |
post.deleteRecord(); | |
//persist change to your backend server | |
post.save().then( | |
function() { | |
this.transitionTo('posts.index'); | |
}, | |
function(error) { | |
// work with person that failed to save | |
if (error.status == 422) { | |
alert("Validation error"); | |
} else { | |
alert("Something went wrong, try again"); | |
} | |
} | |
); | |
} | |
} | |
}); | |
EmBlog.PostsIndexRoute = Ember.Route.extend({ | |
model: function(){ | |
return this.store.find('post'); | |
}, | |
setupController: function(controller, model){ | |
this._super(controller, model); | |
//controller.set('content', model); | |
} | |
}); | |
EmBlog.PostsNewRoute = Ember.Route.extend({ | |
model: function(){ | |
return this.store.createRecord('post'); | |
}, | |
exit: function() { | |
this._super(); | |
this.get('currentModel').rollback(); | |
}, | |
actions: { | |
save: function(post) { | |
post.save().then( | |
function(){ | |
this.transitionToRoute('post.index', post); | |
}, | |
function(error){ | |
if (error.status == 422) { | |
alert("Validation error"); | |
} else { | |
alert("an error occured -- Pls try again") | |
} | |
} | |
); | |
}, | |
cancel: function() { | |
this.transitionTo('posts.index'); | |
} | |
} | |
}); | |
EmBlog.PostCommentsRoute = Ember.Route.extend({ | |
setupController: function(controller, model) { | |
comments = this.controllerFor('post').get('comments'); | |
controller.set('content', comments); | |
}, | |
actions: { | |
removeComment: function(comment) { | |
//deletes record from ember-data store | |
comment.deleteRecord(); | |
//persist change to your backend server | |
comment.save().then( | |
function() { | |
this.controllerFor('postNewComment').set('isEditing', false); | |
this.transitionTo('post.index'); | |
}, | |
function (error){ | |
// work with person that failed to save | |
//comment.rollback(); | |
alert("An error occured - Please try again"); | |
} | |
); | |
} | |
} | |
}); | |
EmBlog.PostNewCommentRoute = Ember.Route.extend({ | |
model: function() { | |
var post = this.modelFor('post'); | |
return this.store.createRecord('comment', {post: post}); | |
}, | |
setupcontroller: function( controller, model) { | |
this._super(); | |
controller.set('content', model); | |
controller.addComment(); | |
}, | |
actions: { | |
save: function(comment){ | |
comment.save().then( | |
function() { | |
this.controllerFor('postNewComment').set('isAddingNew', false); | |
this.transitionTo('post.index'); | |
}, | |
function(error) { | |
// work with person that failed to save | |
comment.rollback(); | |
alert("An error occured - Please try again"); | |
} | |
); | |
}, | |
cancel: function(comment) { | |
this.controllerFor('postNewComment').set('isAddingNew', false); | |
this.transitionTo('post.index'); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment