From f15e6f9afd425e11f6c1e17f3dfac0b5eece48cb Mon Sep 17 00:00:00 2001 From: Justin Manley Date: Thu, 4 Sep 2014 11:32:44 -0400 Subject: [PATCH] Refactor MapKnitter.Resources#_createResource in mapknitter.js and AnnotationsController#create to successfully create new annotations on well-formed AJAX request with content-type: application/json. --- app/assets/javascripts/mapknitter.js | 32 +++++++++++++---------- app/controllers/annotations_controller.rb | 30 +++++++++++++++------ app/views/annotations/index.json.erb | 2 +- app/views/annotations/show.json.erb | 14 ++++++++++ 4 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 app/views/annotations/show.json.erb diff --git a/app/assets/javascripts/mapknitter.js b/app/assets/javascripts/mapknitter.js index 1ddacdac..d1c7326d 100644 --- a/app/assets/javascripts/mapknitter.js +++ b/app/assets/javascripts/mapknitter.js @@ -86,20 +86,23 @@ MapKnitter.Resources = MapKnitter.Class.extend({ _createResource: function(resource, dest) { /* POST to /maps/<%= this._map_id %>/<%= this._name %> */ - var data = { map_id: this._map_id }, + var data = {}, token = $("meta[name='csrf-token']").attr("content"); data[this._name] = this.toJSON(resource); return jQuery.ajax({ - url: this._resourcesUrl, - data: data, - type: 'POST', - beforeSend: function(xhr) { + url: this._resourcesUrl, + data: JSON.stringify(data), + contentType: 'application/json', + type: 'POST', + beforeSend: function(xhr) { + /* Need to pass the csrf token in order to maintain the user session. */ xhr.setRequestHeader('X-CSRF-Token', token); + // xhr.setRequestHeader('Content-Type', 'application/json'); }, - success: function(data) { dest = data; }, - error: function(jqXHR, status, thrownError) { console.log(thrownError); } + success: function(data) { dest = data; }, + error: function(jqXHR, status, thrownError) { console.log(thrownError); } }); } @@ -116,6 +119,7 @@ MapKnitter.Resources = MapKnitter.Class.extend({ } })(); MapKnitter.Annotations.include({ + initialize: function(options) { MapKnitter.Resources.prototype.initialize.call(this, options); @@ -158,7 +162,7 @@ MapKnitter.Annotations.include({ text: this._getContent(annotation), }; - /* If the annotationa already exists in the database. */ + /* If the annotation already exists in the database. */ if (annotation._mapknitter_id) { json.id = annotation._mapknitter_id; } @@ -172,16 +176,16 @@ MapKnitter.Annotations.include({ coord; if (annotation.getLatLng) { - latlngs = [annotation.getLatLng()]; + coord = annotation.getLatLng(); + coordinates = [coord.lng, coord.lat]; } else if (annotation.getLatLngs) { latlngs = annotation.getLatLngs(); + for (var i = 0; i < latlngs.length; i++) { + coord = latlngs[i]; + coordinates.push([coord.lng, coord.lat]); + } } - for (var i = 0; i < latlngs.length; i++) { - coord = latlngs[i]; - coordinates.push([coord.lng, coord.lat]); - } - return coordinates; }, diff --git a/app/controllers/annotations_controller.rb b/app/controllers/annotations_controller.rb index f63a19e2..275387d0 100644 --- a/app/controllers/annotations_controller.rb +++ b/app/controllers/annotations_controller.rb @@ -1,27 +1,41 @@ +require 'json' + class AnnotationsController < ApplicationController - before_filter :require_user, :except => [ :index, :show ] + # before_filter :require_user, :except => [ :index, :show ] + before_filter :find_map def index - @map = Map.find params[:map_id] render :file => 'annotations/index.json.erb', :content_type => 'application/json' end def create - @map = Map.find params[:map_id] - @annotation = @map.annotations.create params[:annotation] - @annotation.user_id = current_user.id - - if @annotation.save - respond_with(@map, @annotation, 201) + params[:annotation][:coordinates] = params[:annotation][:coordinates].to_json.to_s + respond_to do |format| + format.json { + @annotation = @map.annotations.create params[:annotation] + @annotation.user_id = current_user.id + redirect_to map_annotation_url(@map, @annotation) + } end end def show + @annotation = Annotation.find params[:id] + render :file => 'annotations/show.json.erb', :content_type => 'application/json' end def update end def destroy + @annotation = Annotation.find params[:id] + # if current_user.can_delete?(@annotation) + @annotation.delete + # end end + + def find_map + @map = Map.find params[:map_id] + end + end \ No newline at end of file diff --git a/app/views/annotations/index.json.erb b/app/views/annotations/index.json.erb index 71067099..48fd6bc8 100644 --- a/app/views/annotations/index.json.erb +++ b/app/views/annotations/index.json.erb @@ -4,7 +4,7 @@ "type": "Feature", "geometry": { "type": "Point", - "coordinates": "<%= annotation.coordinates %>" + "coordinates": <%= annotation.coordinates %> }, "properties": { "annotation_type": "<%= annotation.annotation_type %>", diff --git a/app/views/annotations/show.json.erb b/app/views/annotations/show.json.erb new file mode 100644 index 00000000..d2004f16 --- /dev/null +++ b/app/views/annotations/show.json.erb @@ -0,0 +1,14 @@ +{ + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": <%= @annotation.coordinates %> + }, + "properties": { + "annotation_type": "<%= @annotation.annotation_type %>", + "style": "<%= @annotation.style %>", + "text": "<%= @annotation.text %>", + "id": <%= @annotation.id %>, + "url": "<%= url_for([@map, @annotation]) %>" + } +} \ No newline at end of file