Skip to content

Commit

Permalink
Refactor posting status into a view
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed May 14, 2012
1 parent aaae43f commit 30baae5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
3 changes: 2 additions & 1 deletion public/index.html
Expand Up @@ -9,6 +9,7 @@
<script src="js/setup.js"></script>
<script src="js/models/status.js"></script>
<script src="js/collections/statuses.js"></script>
<script src="js/views/post_status.js"></script>
<script src="js/app.js"></script>
</head>
<body>
Expand All @@ -18,7 +19,7 @@
<a class="brand" href="#">Monologue</a>
</div>
</div>
<form class="row">
<form id="new-status" class="row">
<label for="status_text">Status</label>
<textarea id="status_text" name="status" placeholder="What are your inner most thoughts?"></textarea>
<button type="submit" class="btn">Post Update</button>
Expand Down
5 changes: 1 addition & 4 deletions public/js/app.js
@@ -1,10 +1,7 @@
jQuery(function($) {
var statuses = new Monologue.Collection.Statuses();

$('form').on('submit', function() {
statuses.create({text: $(this).find('textarea').val()});
return false;
});
new Monologue.View.PostStatus({el: $('#new-status'), collection: statuses});

var append = function(status) {
$('#statuses').append('<li>' + status.get('text') + '</li>');
Expand Down
18 changes: 18 additions & 0 deletions public/js/views/post_status.js
@@ -0,0 +1,18 @@
// View to manage posting a new status.
//
// Dependencies:
// * el - A form element with a textarea
// * collection - the collection that the status will be created in
Monologue.View.PostStatus = Backbone.View.extend({
events: {
"submit": "submit"
},

submit: function(e) {
e.preventDefault();
var $input = this.$el.find('textarea');
this.collection.create({text: $input.val()});
$input.val('');
return false;
}
});
26 changes: 26 additions & 0 deletions spec/views/post_status_spec.js
@@ -0,0 +1,26 @@
require('/js/views/post_status.js');

describe("Monologue.View.PostStatus", function() {
var view, $el, collection;

beforeEach(function() {
$el = $("<form><textarea>See, it's not so hard!</textarea></form>");
collection = new (Backbone.Collection.extend({url: '/mock'}));
// prevent collection from trying to sync with the server
spyOn(collection, 'create');

view = new Monologue.View.PostStatus({el: $el, collection: collection});
});

describe("submitting the form", function() {
it("creates status when submitting form", function() {
$el.trigger('submit');
expect(collection.create).toHaveBeenCalledWith({text: "See, it's not so hard!"});
});

it("clears the textarea", function() {
$el.trigger('submit');
expect($el.find('textarea').val()).toEqual('');
});
});
});

0 comments on commit 30baae5

Please sign in to comment.