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.

This commit is contained in:
Justin Manley
2014-09-04 11:32:44 -04:00
parent 32b06c3a5a
commit f15e6f9afd
4 changed files with 55 additions and 23 deletions

View File

@@ -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;
},

View File

@@ -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

View File

@@ -4,7 +4,7 @@
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": "<%= annotation.coordinates %>"
"coordinates": <%= annotation.coordinates %>
},
"properties": {
"annotation_type": "<%= annotation.annotation_type %>",

View File

@@ -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]) %>"
}
}