Permalink
Browse files

Changes to accomodate changing javascript load based on controller ac…

…tion and other stuff
  • Loading branch information...
1 parent ec6bb07 commit b74b58e13283a65b94abcc64eac6ccb27978d98d @ruprict committed Dec 18, 2011
View
34 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)
View
3 app/assets/javascripts/application.js
@@ -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)
View
4 app/assets/javascripts/router.js.coffee → ...ripts/routers/eventsIndexRouter.js.coffee
@@ -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()
View
0 spec/javascripts/views/eventFormView_spec.js → ...cripts/routers/eventsShowRouter.js.coffee
File renamed without changes.
View
16 ...javascripts/views/eventEditView.js.coffee → ...vascripts/views/createEventView.js.coffee
@@ -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) ->
View
2 app/views/layouts/application.html.haml
@@ -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
View
9 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>
View
18 spec/javascripts/routers/router_spec.js → ...scripts/routers/eventsIndexRouter_spec.js
@@ -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});
@@ -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() {
@@ -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();
+ });
});
});
View
34 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.