Skip to content

Commit

Permalink
edit seems to work but triggers an error
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchiea committed Oct 10, 2012
1 parent d0c6c22 commit 073fe57
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
32 changes: 31 additions & 1 deletion public/js/script.js
Expand Up @@ -129,8 +129,10 @@ $(function(){
editPost: function() {
var title = this.model.get('title');
var text = this.model.get('text');
var id = this.model.get('_id')
window.$('#title').val(title);
window.$('#text').val(text);
window.$('#post-id').val(id);
}

});
Expand Down Expand Up @@ -183,7 +185,7 @@ $(function(){
el: $("#admin"),

events: {
"click #save-post": "createOnSubmit"
"click #save-post": "findOrCreateOnSubmit"
},

initialize: function() {
Expand All @@ -202,6 +204,34 @@ $(function(){
library.create({title: title, text: text});
$title.val('');
$text.val('');
library.fetch(); // I think this should be event triggered...
},

findOrCreateOnSubmit: function (){

// this would be better if instread of create it was create or save

$title = this.$('#title');
$text = this.$('#text');
$post-id = this.$('#post-id');

var title = $title.val();
var text = $text.val();

if ($post-id.val().length != 0) { // id exists
var id = $post-id.val();
var post = library.get(id);
post.save({title: title, text: text});
}

else {
library.create({title: title, text: text});
}

$title.val('');
$text.val('');
$post-id.val('');

library.fetch(); // I think this should be event triggered...
}

Expand Down
1 change: 1 addition & 0 deletions views/admin.haml
Expand Up @@ -7,6 +7,7 @@
%h2
New Post
%div#new-post
%input#post-id{'type' => 'hidden'}
%input#title{"placeholder" => "What's on your mind?", "type" => "text", :name => "title"}/
%br/
%textarea#text{"placeholder" => "Post body here"}
Expand Down

0 comments on commit 073fe57

Please sign in to comment.