PageRenderTime 63ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/google-maps-studentive/web-app/js/backbone/models/place.js

https://bitbucket.org/lojzatran/google-maps-studentive
JavaScript | 115 lines | 89 code | 7 blank | 19 comment | 7 complexity | 2e3ec71ad13069d1c841c256258141db MD5 | raw file
  1. /**
  2. * model pro místo
  3. */
  4. $(function () {
  5. APP = window.APP || {};
  6. APP.PlaceModel = Backbone.Model.extend({
  7. urlRoot:function () {
  8. return window.appPath + "/placeResource/";
  9. },
  10. initialize:function () {
  11. this.on("error", this.showErrors);
  12. },
  13. defaults:{
  14. id:null,
  15. name:"",
  16. address:{
  17. number:0,
  18. street:"",
  19. city:"",
  20. country:""
  21. },
  22. latitude:0,
  23. longitude:0,
  24. notes:"",
  25. marker:null
  26. },
  27. /**
  28. * smaže označení místa z mapy
  29. */
  30. removeMarkerFromMap:function () {
  31. var marker = this.get("marker");
  32. marker.setMap(null);
  33. },
  34. /**
  35. * vyhledá a vrátí adresu ze souřadnic
  36. */
  37. getAddressFromLatLng:function () {
  38. var lat = this.get("latitude");
  39. var lng = this.get("longitude");
  40. var latLng = new google.maps.LatLng(lat, lng);
  41. var geocoder = new google.maps.Geocoder();
  42. var self = this;
  43. geocoder.geocode({'latLng':latLng}, function (results, status) {
  44. if (status == google.maps.GeocoderStatus.OK) {
  45. if (results[0]) {
  46. var resultJson = $.evalJSON($.toJSON(results[0]));
  47. self.parseAddress(resultJson);
  48. APP.placeView.render(self);
  49. }
  50. } else {
  51. console.log("Geocoder failed due to: " + status);
  52. console.log($.toJSON(status));
  53. }
  54. });
  55. },
  56. /**
  57. * z JSON vrácené ze servery parsuje adresu
  58. * a uloží do modelu
  59. * @param resultJson
  60. * @return {Object}
  61. */
  62. parseAddress:function (resultJson) {
  63. var addressArray = resultJson.address_components;
  64. var self = this;
  65. var currentAddress = {};
  66. $.each(addressArray, function (index, element) {
  67. var types = element.types[0];
  68. switch (types) {
  69. case "street_number":
  70. currentAddress.number = element.long_name;
  71. break;
  72. case "route":
  73. currentAddress.street = element.long_name;
  74. break;
  75. case "locality":
  76. currentAddress.city = element.long_name;
  77. break;
  78. case "country":
  79. currentAddress.country = element.long_name;
  80. break;
  81. }
  82. });
  83. self.set({address:currentAddress}, {silent:true});
  84. return currentAddress;
  85. },
  86. validate:function (attrs) {
  87. if (!attrs.name) {
  88. return APP.ErrorModel.createErrorModel({errorMessage:"Chybí název místa"});
  89. }
  90. },
  91. showErrors:function (model, errorObject) {
  92. APP.placeView.onError(this, errorObject);
  93. return false;
  94. }
  95. });
  96. APP.placeModel = new APP.PlaceModel();
  97. /**
  98. * kolekce míst
  99. * @type {*}
  100. */
  101. APP.PlaceCollection = Backbone.Collection.extend({
  102. initialize:function () {
  103. },
  104. model:APP.PlaceModel,
  105. url:window.appPath + "/placeResource"
  106. });
  107. APP.placeCollection = new APP.PlaceCollection();
  108. });