Permalink
Browse files

Last article

  • Loading branch information...
1 parent 84301f9 commit 5e18d5633b9fcc1a7ce7ff0a3ca0820ef89ace01 @ruprict committed Jan 24, 2012
@@ -1,4 +0,0 @@
-# Place all the behaviors and hooks related to the matching controller here.
-# All this logic will automatically be available in application.js.
-# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
-
@@ -1,3 +0,0 @@
-# Place all the behaviors and hooks related to the matching controller here.
-# All this logic will automatically be available in application.js.
-# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -16,6 +16,36 @@ App.MapProviders.Leaflet =
map.addLayer(layer)
setViewForMap: (map, point, zoomLevel) ->
map.setView(point, zoomLevel)
+ addOccasion: (map,occ) ->
+ if not @layerGroup?
+ @layerGroup = new L.LayerGroup()
+ map.addLayer(@layerGroup)
+ ll = new L.LatLng(
+ parseFloat(occ.get("latitude")),
+ parseFloat(occ.get("longitude"))
+ )
+ marker = new L.Marker(ll)
+ marker.bindPopup(occ.get("note"))
+ @layerGroup.addLayer(marker)
+ addNewOccasionMarker: (map, e, content) ->
+ ll = new L.LatLng(e.latlng.lat,e.latlng.lng)
+ @marker = new L.Marker(ll)
+ @marker.bindPopup(content)
+ map.addLayer(@marker)
+ @marker.openPopup()
+ $("#occasion_latitude").val(e.latlng.lat)
+ $("#occasion_longitude").val(e.latlng.lng)
+ $("#occasion_occurred_at").val(new Date())
+ addClickHandler: (map, callback) ->
+ map.on('click', callback)
+ hidePopup: (map)->
+ map.closePopup()
+ map.removeLayer(@marker)
+ removeAllMarkers: (map) ->
+ if @layerGroup?
+ @layerGroup.clearLayers()
+
+
@@ -3,9 +3,15 @@ __super = Backbone.Model.prototype
App.Event = Backbone.Model.extend
idAttribute: "_id"
url: ->
- if this.id? == undefined then "/events" else "/events/" + @id
- url << "
- then "/events" else "/events/" + @id
+ url = ""
+ if @collection?
+ url += @collection.url
+ else
+ url += "/events"
+
+ if @id?
+ url += "/#{@id}"
+ url
validate: (attrs)->
if !attrs.name
"must have a valid name."
@@ -3,8 +3,8 @@ __super = Backbone.Model.prototype
App.Occasion = Backbone.Model.extend
idAttribute: "_id"
url: ->
- url = '/events/' + App.event_id + '/occasions'
- if this.id? != undefined
+ url = '/events/' + App.event_id + '/occasions'
+ if this.id?
url += '/' + @id
url
validate: (attrs)->
@@ -1,3 +0,0 @@
-# Place all the behaviors and hooks related to the matching controller here.
-# All this logic will automatically be available in application.js.
-# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -6,7 +6,6 @@ App.EventRouter = Backbone.Router.extend
@occasionListView = new App.OccasionListView
collection: window.occasionCollection or= new App.OccasionsCollection()
@occasionListView.render()
- @createOccasionView = new App.CreateOccasionView()
if $('#map').length > 0
@mapView = new App.MapView(App.MapProviders.Leaflet)
@mapView.render()
@@ -1,8 +1,10 @@
App or= {}
App.CreateOccasionView = Backbone.View.extend(
- el: "#edit_occasion"
initialize: ->
- @form = $(this.el).find("form")
+ @template = $(".edit_occasion").clone().children("form").end()
+ $(@el).append(@template)
+ $(@el).find('div').show()
+ @form = $(@el).find("form")
events:
"submit form" : "handleFormSubmission"
handleFormSubmission: (e) ->
@@ -17,6 +19,7 @@ App.CreateOccasionView = Backbone.View.extend(
occasion.save()
else
occasionCollection.create(occasion)
+ $(this.el).trigger('map:occasionAdded', occasion)
)
@@ -16,4 +16,3 @@ App.EventListView = Backbone.View.extend
view = new App.EventView({model: ev})
$(@el ).append(view.render().el)
-
@@ -1,17 +1,39 @@
App or= {}
App.MapView = Backbone.View.extend
- el: "div#map",
- initialize: (mapProvider) ->
+ el: "div#map"
+ events:
+ "map:occasionAdded" : "handleSubmit"
+ initialize: (mapProvider) ->
@mapFactory = mapProvider
+ @collection = window.occasionCollection
+ @collection.bind("add", @addOccasion, this)
+ @collection.bind("all", @drawOccasions, this)
addBaseLayer: ->
@baseLayer = new @mapFactory.BaseMapLayer()
@mapFactory.addLayerToMap(@map, @baseLayer)
setInitialView: ->
@mapFactory.setViewForMap(@map, @mapFactory.Point(51.505, 0.09), 13)
render: ->
@map = new @mapFactory.Map(@el.id)
+ @mapFactory.addClickHandler(@map,@newOccasion.bind(@))
@addBaseLayer()
@setInitialView()
+ @drawOccasions()
+ drawOccasions: () ->
+ self = this
+ @mapFactory.removeAllMarkers(@map)
+ window.occasionCollection.each((occ)->
+ self.mapFactory.addOccasion(self.map,occ)
+ )
+ newOccasion: (e) ->
+ view = new App.CreateOccasionView()
+ view.render()
+ @mapFactory.addNewOccasionMarker(@map, e,view.el )
+ addOccasion: (occ) ->
+ @mapFactory.addOccasion(@map, occ)
+ handleSubmit: ->
+ @mapFactory.hidePopup(@map)
+
@@ -6,23 +6,20 @@
%ul#occasionsList.four.columns.omega
- for occasion in @event.occasions
%script
- bootstrapOccasions.push(new App.Occasion({ latitude: "#{occasion.latitude}", longitude: "#{occasion.longitude}:", note: "#{occasion.note}", occurred_at: "#{occasion.occurred_at}", _id: "#{occasion._id}", }));
+ bootstrapOccasions.push(new App.Occasion({ latitude: "#{occasion.latitude}", longitude: "#{occasion.longitude}", note: "#{occasion.note}", occurred_at: "#{occasion.occurred_at}", _id: "#{occasion._id}", }));
%li
= render :partial => "occasions/occasion", :locals => {:occasion => occasion}
%div.clear
-%div#edit_occasion
- = form_for [@event, current_user.events.find(@event.id).occasions.build()] do |f|
- %div.coordinate_input
- = f.label :latitude
- = f.text_field :latitude
- %div.coordinate_input
- = f.label :longitude
- = f.text_field :longitude
- %div.date_field
- = f.label :occurred_at
- = f.text_field :occurred_at
- %div.note_field
- = f.label :note
- = f.text_area :note
- = f.submit "Add"
+ %div.edit_occasion{ :style => "display:none"}
+ = form_for [@event, current_user.events.find(@event.id).occasions.build()] do |f|
+ %div.coordinate_input
+ = f.hidden_field :latitude
+ %div.coordinate_input
+ = f.hidden_field :longitude
+ %div.date_field
+ = f.hidden_field :occurred_at
+ %div.note_field
+ = f.label :note
+ = f.text_area :note
+ = f.submit "Add"
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -1,12 +1,13 @@
describe("Occasion model", function() {
beforeEach(function() {
- var ev = new App.Event({_id: 222});
+ this.ev = new App.Event({_id: 222});
+ App.event_id = this.ev.id;
this.occ = new App.Occasion({
latitude: 35.33,
longitude: -88.44,
note: "Test",
_id: 11,
- event: ev
+ event: this.ev
});
});
describe("when instantiated with attributes", function() {
@@ -1,21 +1,50 @@
describe("MapView", function() {
beforeEach(function() {
loadFixtures("map.html");
+
this.mapProviderSpy = sinon.stub(App.MapProviders.Leaflet);
this.view = new App.MapView(this.mapProviderSpy);
});
+ this.afterEach(function() {
+ App.MapProviders.Leaflet.Map.restore();
+ App.MapProviders.Leaflet.BaseMapLayer.restore();
+ App.MapProviders.Leaflet.Point.restore();
+ App.MapProviders.Leaflet.addLayerToMap.restore();
+ App.MapProviders.Leaflet.setViewForMap.restore();
+ App.MapProviders.Leaflet.addOccasion.restore();
+ App.MapProviders.Leaflet.addClickHandler.restore();
+ App.MapProviders.Leaflet.addNewOccasionMarker.restore();
+ });
+
it("should use the #map element by default", function() {
expect(this.view.el.id).toEqual("map");
});
- describe("When the new Occasion form is presented to the user", function() {
+ describe("render", function() {
+ beforeEach(function() {
+ this.mock = this.mapProviderSpy
+ });
+ it("should add a click handler to the map", function() {
+ this.view.render();
+ expect(this.mapProviderSpy.addClickHandler).toHaveBeenCalled();
+ });
+ });
+
+ describe("When a new Occasion is requested", function() {
beforeEach(function() {
- this.view.showOccasionForm();
+ var e = {
+ latlng: {lat: 100.00, lng: 100.00}
+ };
+ this.view.newOccasion(e);
});
it("should have a form", function() {
expect($("#new_occasion_form").length).toEqual(1);
});
+
+ it("should add a marker to the map", function () {
+ expect(this.mapProviderSpy.addNewOccasionMarker).toHaveBeenCalled();
+ });
});
});

0 comments on commit 5e18d56

Please sign in to comment.