MS DC posting from post/new werks

This commit is contained in:
Dennis Collinson 2012-03-06 16:26:19 -08:00
parent ab35f2bf3d
commit bf4dabc4f4
9 changed files with 84 additions and 21 deletions

View file

@ -8,5 +8,5 @@ Feature: Creating a new post
When I trumpet When I trumpet
And I write "Rectangles are awesome" And I write "Rectangles are awesome"
And I press "Share" And I press "Share"
# When I go to the stream page When I go to "/stream"
# Then I should see "Rectangles are awesome" as the first post in my stream Then I should see "Rectangles are awesome" as the first post in my stream

View file

@ -1 +1,11 @@
app.models.StatusMessage = app.models.Post.extend({ }); app.models.StatusMessage = app.models.Post.extend({
url : function(){
return this.isNew() ? '/status_messages' : '/posts/' + this.get("id");
},
mungeAndSave : function(){
var mungedAttrs = {status_message : _.clone(this.attributes), aspect_ids : ["public"]}
this.save(mungedAttrs)
}
});

View file

@ -4,9 +4,13 @@ app.pages.PostNew = app.views.Base.extend({
subviews : { "#new-post" : "postForm"}, subviews : { "#new-post" : "postForm"},
initialize : function(){ initialize : function(){
console.log("In the page") this.model = new app.models.StatusMessage()
this.model = new app.models.Post()
this.postForm = new app.views.PostForm({model : this.model}) this.postForm = new app.views.PostForm({model : this.model})
this.model.bind("setFromForm", this.saveModel, this)
},
saveModel : function(){
this.model.mungeAndSave();
} }
}) })

View file

@ -1,4 +1,4 @@
<form class="new-post"> <form class="new-post">
<label>text<textarea class="text"/></label> <label>text<textarea class="text"/></label>
<input type="submit" value="Share" class="btn-primary"></input> <input type="submit" class="btn-primary" value="Share"></input>
</form> </form>

View file

@ -1,12 +1,25 @@
app.views.PostForm = app.views.Base.extend({ app.views.PostForm = app.views.Base.extend({
templateName : "post-form", templateName : "post-form",
initialize : function(){ events :{
console.log("In the form") 'submit form' : 'setModelAttributes'
}, },
postRenderTemplate: function(){ formAttrs : {
console.log("I'm getting rendered") ".text" : "text"
},
setModelAttributes : function(evt){
if(evt){ evt.preventDefault(); }
var form = this.$("form");
function setValueFromField(memo, attribute, selector){
memo[attribute] = form.find(selector).val()
return memo
} }
this.model.set(_.inject(this.formAttrs, setValueFromField, {}))
this.model.trigger("setFromForm")
}
}); });

View file

@ -6,17 +6,13 @@ describe("app", function() {
it("returns false if the current_user isn't set", function() { it("returns false if the current_user isn't set", function() {
app._user = undefined; app._user = undefined;
expect(app.user()).toEqual(false); expect(app.user()).toEqual(false);
}); });
});
describe('currentUser', function(){
it("sets the user if given one and returns the current user", function() { it("sets the user if given one and returns the current user", function() {
expect(app.currentUser.authenticated()).toBeFalsy() expect(app.user()).toBeFalsy()
app.user({name: "alice"}); app.user({name: "alice"});
expect(app.user().get("name")).toEqual("alice"); expect(app.user().get("name")).toEqual("alice");
}); });
}); });
}); })

View file

@ -0,0 +1,12 @@
describe("app.models.StatusMessage", function(){
describe("#url", function(){
it("is /status_messages when its new", function(){
var post = new app.models.StatusMessage()
expect(post.url()).toBe("/status_messages")
})
it("is /posts/id when it has an id", function(){
expect(new app.models.StatusMessage({id : 5}).url()).toBe("/posts/5")
})
})
})

View file

@ -6,4 +6,12 @@ describe("app.pages.PostNew", function(){
it("renders", function(){ it("renders", function(){
this.page.render(); this.page.render();
}) })
context("when the model receives setFromForm", function(){
it("it calls mungeAndSave", function(){
spyOn(this.page.model, "mungeAndSave")
this.page.model.trigger("setFromForm")
expect(this.page.model.mungeAndSave).toHaveBeenCalled();
})
})
}); });

View file

@ -4,7 +4,27 @@ describe("app.views.PostForm", function(){
this.view = new app.views.PostForm({model : this.post}) this.view = new app.views.PostForm({model : this.post})
}) })
it("renders", function(){ describe("rendering", function(){
beforeEach(function(){
this.view.render() this.view.render()
}) })
describe("submitting a valid form", function(){
beforeEach(function(){
this.view.$("form .text").val("Oh My")
})
it("instantiates a post on form submit", function(){
this.view.$("form").submit()
expect(this.view.model.get("text")).toBe("Oh My")
})
it("triggers a 'setFromForm' event", function(){
var spy = jasmine.createSpy();
this.view.model.bind("setFromForm", spy);
this.view.$("form").submit();
expect(spy).toHaveBeenCalled();
})
})
})
}) })