Permalink
Browse files

Changes for Giving Events a Backbone

  • Loading branch information...
1 parent 5b78889 commit ec6bb07a70800fc74f1b67933ea5e9d46b848d10 @ruprict committed Dec 18, 2011
@@ -2,5 +2,7 @@ window.App =
start: ->
window.eventCollection = new App.EventsCollection(bootstrapEvents)
new App.Router()
+ Backbone.history.start
+ root: "/events"
@@ -8,7 +8,7 @@
//= require jquery_ujs
//= require vendor
//= require ./app
-//= require ./appRouter
+//= require ./router
//= require_tree ./lib
//= require_tree ./models
//= require_tree ./collections
@@ -0,0 +1,4 @@
+App.TemplateProvider =
+ applyTemplate: (object_type, template_name, object) ->
+ window.HAML.templates[object_type][template_name](object.attributes)
+
@@ -1,6 +1,7 @@
__super = Backbone.Model.prototype
App.Event = Backbone.Model.extend
+ idAttribute: "_id"
url: ->
if (this.collection)
return __super.url.call(@)
@@ -1,9 +1,12 @@
App or= {}
App.Router = Backbone.Router.extend
- initialize: ->
- @eventListView = new App.EventListView({collection: window.eventCollection})
+ routes:
+ "" : "index"
+ index: ->
+ @eventListView = new App.EventListView({collection: window.eventCollection or= new App.EventsCollection()})
@eventListView.render()
@eventEditView = new App.EventEditView()
if $('#map').length > 0
@mapView = new App.MapView(App.MapProviders.Leaflet)
+ @mapView.render()
@@ -3,7 +3,6 @@ App.MapView = Backbone.View.extend
el: "div#map",
initialize: (mapProvider) ->
@mapFactory = mapProvider
- @render()
addBaseLayer: ->
@baseLayer = new @mapFactory.BaseMapLayer()
@mapFactory.addLayerToMap(@map, @baseLayer)
@@ -0,0 +1,27 @@
+describe("EventsCollection", function() {
+ beforeEach(function() {
+ this.eventStub = sinon.stub(App, "Event");
+
+ this.model = new (Backbone.Model.extend({
+ idAttribute: "_id"
+ }))
+ ({
+ _id: 5,
+ name: "Test Event"
+ });
+ this.eventCollection = new App.EventsCollection();
+ this.eventCollection.add(this.model);
+ });
+
+ afterEach(function() {
+ this.eventStub.restore();
+ });
+
+ it("should add a model", function() {
+ expect(this.eventCollection.length).toEqual(1);
+ });
+
+ it("should find a model by id", function(){
+ expect(this.eventCollection.get(5).id).toEqual(5);
+ });
+});
@@ -35,4 +35,10 @@ describe("Event model", function() {
expect(this.event.url()).toEqual("/loccasions");
});
});
+ describe("id", function() {
+ it("should use _id for the id attribute", function() {
+ var ev = new App.Event({_id:44, name: "MongoEvent"});
+ expect(ev.id).toEqual(44);
+ });
+ });
});
@@ -0,0 +1,43 @@
+describe("App.Router", function() {
+ describe("routes", function() {
+ beforeEach(function() {
+ this.router = new App.Router();
+ this.routeSpy = sinon.spy();
+ try {
+ Backbone.history.start({silent: true});
+ } catch(e) {
+ console.dir(e);
+ }
+ this.router.navigate("away");
+ });
+ it("should map the blank route to index", function() {
+ this.router.bind("route:index", this.routeSpy);
+ this.router.navigate("", true);
+ expect(this.routeSpy).toHaveBeenCalledOnce();
+ expect(this.routeSpy).toHaveBeenCalledWith();
+ });
+ });
+ 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.index();
+ });
+
+ afterEach(function() {
+ App.MapView.restore();
+ App.EventListView.restore();
+ });
+
+ it("should create the EventListView", function() {
+ expect(this.eventListViewSpy).toHaveBeenCalled();
+ });
+
+ it("should create the MapView", function() {
+ expect(this.mapViewSpy).toHaveBeenCalled();
+ });
+ });
+});
@@ -1,8 +1,11 @@
describe("MapView", function() {
- it("should use the #map element by default", function() {
+ beforeEach(function() {
loadFixtures("map.html");
- var mapProviderSpy = sinon.stub(App.MapProviders.Leaflet);
- var view = new App.MapView(mapProviderSpy);
- expect(view.el.id).toEqual("map");
+ this.mapProviderSpy = sinon.stub(App.MapProviders.Leaflet);
+ this.view = new App.MapView(this.mapProviderSpy);
+ });
+
+ it("should use the #map element by default", function() {
+ expect(this.view.el.id).toEqual("map");
});
});

0 comments on commit ec6bb07

Please sign in to comment.