Skip to content

Commit

Permalink
Changes to accomodate changing javascript load based on controller ac…
Browse files Browse the repository at this point in the history
…tion and other stuff
  • Loading branch information
ruprict committed Dec 18, 2011
1 parent ec6bb07 commit b74b58e
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 24 deletions.
34 changes: 29 additions & 5 deletions app/assets/javascripts/app.js.coffee
@@ -1,8 +1,32 @@
window.App =
start: ->
window.eventCollection = new App.EventsCollection(bootstrapEvents)
new App.Router()
Backbone.history.start
root: "/events"
common:
init: ->
events:
init: ->
index: ->
window.eventCollection = new App.EventsCollection(bootstrapEvents)
new App.EventsRouter()
Backbone.history.start
root: "/events"
show:
console.log("You're in event show")

UTIL =
exec: ( controller, action )->
ns = App
action or= "init"

if ( controller != "" && ns[controller] && typeof ns[controller][action] == "function" )
ns[controller][action]()

init: ->
body = document.body
controller = body.getAttribute( "data-controller" )
action = body.getAttribute( "data-action" )

UTIL.exec( "common" )
UTIL.exec( controller )
UTIL.exec( controller, action )

$(UTIL.init)

3 changes: 1 addition & 2 deletions app/assets/javascripts/application.js
Expand Up @@ -8,11 +8,10 @@
//= require jquery_ujs
//= require vendor
//= require ./app
//= require ./router
//= require_tree ./routers
//= require_tree ./lib
//= require_tree ./models
//= require_tree ./collections
//= require_tree ./views
//= require_tree ./templates

$(App.start)
@@ -1,11 +1,11 @@
App or= {}
App.Router = Backbone.Router.extend
App.EventsIndexRouter = Backbone.Router.extend
routes:
"" : "index"
index: ->
@eventListView = new App.EventListView({collection: window.eventCollection or= new App.EventsCollection()})
@eventListView.render()
@eventEditView = new App.EventEditView()
@createEventView = new App.CreateEventView()
if $('#map').length > 0
@mapView = new App.MapView(App.MapProviders.Leaflet)
@mapView.render()
Expand Down
@@ -1,23 +1,23 @@
App or= {}
App.EventEditView = Backbone.View.extend(
App.CreateEventView = Backbone.View.extend(
el: "#edit_event"
initialize: ->
@form = $(this.el).find("form")
events:
"submit form" : "handleFormSubmit"

handleFormSubmit: (e)->
"submit form" : "handleFormSubmission"
handleFormSubmission: (e) ->
e.stopPropagation()
evento = new App.Event(@extractEventAttributesFromForm().event)
@createEvent()
false
createEvent: ()->
evento = new App.Event(@parseFormAttributes().event)
has_id = @form.attr("action").match(/\/events\/(\w*)/)
if has_id
evento.id = has_id[1]
evento.save()
else
eventCollection.create(evento)
false
#MOVE THIS
extractEventAttributesFromForm: ->
parseFormAttributes: ->
_.inject(
@form.serializeArray(),
(memo, pair) ->
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.haml
Expand Up @@ -23,7 +23,7 @@
= javascript_include_tag :application
= csrf_meta_tags

%body
%body{:"data-controller" => controller_name, :"data-action" => action_name }
.container
#sign_in.sixteen.columns
%span
Expand Down
9 changes: 9 additions & 0 deletions spec/javascripts/fixtures/eventForm.html
@@ -0,0 +1,9 @@
<div id="edit_event">
<form accept-charset="UTF-8" action="/events" class="new_event" id="new_event" method="post">
<label for="event_name">Name</label>
<input id="event_name" name="event[name]" size="30" type="text" >
<label for="event_description">Description</label>
<input id="event_description" name="event[description]" size="30" type="text">
<input name="commit" type="submit" value="Create Event">
</form>
</div>
@@ -1,7 +1,7 @@
describe("App.Router", function() {
describe("App.EventsIndexRouter", function() {
describe("routes", function() {
beforeEach(function() {
this.router = new App.Router();
this.router = new App.EventsIndexRouter();
this.routeSpy = sinon.spy();
try {
Backbone.history.start({silent: true});
Expand All @@ -20,16 +20,18 @@ describe("App.Router", function() {
describe("index", function() {
beforeEach(function() {
loadFixtures("map.html");
this.router = new App.Router();
this.eventListView = new Backbone.View({});
this.eventListViewSpy = sinon.stub(App, "EventListView").returns(this.eventListView);
this.mapViewSpy = sinon.stub(App, "MapView").returns(this.eventListView);
this.router = new App.EventsIndexRouter();
this.mockView = new Backbone.View({});
this.eventListViewSpy = sinon.stub(App, "EventListView").returns(this.mockView);
this.mapViewSpy = sinon.stub(App, "MapView").returns(this.mockView);
this.createViewSpy = sinon.stub(App, "CreateEventView").returns(this.mockView);
this.router.index();
});

afterEach(function() {
App.MapView.restore();
App.EventListView.restore();
App.CreateEventView.restore();
});

it("should create the EventListView", function() {
Expand All @@ -39,5 +41,9 @@ describe("App.Router", function() {
it("should create the MapView", function() {
expect(this.mapViewSpy).toHaveBeenCalled();
});

it("should create the CreateEventView", function() {
expect(this.createViewSpy).toHaveBeenCalled();
});
});
});
34 changes: 34 additions & 0 deletions spec/javascripts/views/createEventView_spec.js
@@ -0,0 +1,34 @@
describe("CreateEventView", function() {
beforeEach(function() {
loadFixtures("eventForm.html");
this.view = new App.CreateEventView();
this.form = $(this.view.el).find("form")[0];
});
it("should provide a form", function() {
expect($(this.view.el).find("form").length).toEqual(1);
});

describe("creating an event", function() {
beforeEach(function() {
window.eventCollection = new Backbone.Collection();
this.createStub = sinon.stub(window.eventCollection, "create");
$(this.form).find("#event_name").val("Test Event");
$(this.form).find("#event_description").val("Test Event Description");
this.view.createEvent();
});

it("should call create on the EventCollection", function() {
expect(this.createStub).toHaveBeenCalled();
});

});
describe("parsing form attributes", function() {
it("should have the correct attribute values", function() {
$(this.form).find("#event_name").val("Test Event");
$(this.form).find("#event_description").val("Test Event Description");
var attributes = this.view.parseFormAttributes().event;
expect(attributes.name).toEqual("Test Event");
expect(attributes.description).toEqual("Test Event Description");
});
});
});

0 comments on commit b74b58e

Please sign in to comment.