PlaceModel.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * PlaceModel.js defines simple handler for
  3. * place data JSON received from Qype API
  4. *
  5. * PlaceModel functions return single data items
  6. * parsed from JSON for snippets and views
  7. * generating UI
  8. */
  9. /*
  10. * This is a sample of single place data JSON received from Qype API
  11. *
  12. * {"place":{"opening_hours":"Monday, Sunday: closed, Tuesday - Wednesday: 19:00 - 1:00, Thursday - Saturday: 20:00 - 3:00","updated":"2010-08-05T16:55:15+02:00","point":"53.5532,9.95799","closed":false,"image":{"medium":"http://assets3.qype.com/uploads/photos/0175/2419/-365847936_thumb.jpg","medium2x":"http://assets2.qype.com/uploads/photos/0175/2419/-365847936_large.jpg","large":"http://assets3.qype.com/uploads/photos/0175/2419/-365847936_gallery.jpg","small":"http://assets2.qype.com/uploads/photos/0175/2419/-365847936_mini.jpg"},
  13. * "address":{"country_code":"DE","postcode":"22767","city":"Hamburg","housenumber":"2-4","street":"Am Brunnenhof"},"title":"Luba Luft","categories":[{"full_title":{"value":"Cocktail Bars","lang":"en"},"updated":"2009-12-09T10:21:15+01:00","title":{"value":"Cocktail Bars","lang":"en"},"links":[{"href":"http://api.qype.com/v1/place_categories/249","rel":"self"},{"href":"http://api.qype.com/v1/place_categories/22","rel":"http://schemas.qype.com/place_category.parent"}],"id":"tag:api.qype.com,2007-11-02:/places/categories/249",
  14. * "created":"2007-11-02T15:35:46+01:00"},{"full_title":{"value":"Clubs","lang":"en"},"updated":"2009-12-09T10:21:38+01:00","title":{"value":"Clubs","lang":"en"},"links":[{"href":"http://api.qype.com/v1/place_categories/23","rel":"self"},{"href":"http://api.qype.com/v1/place_categories/23/children","rel":"http://schemas.qype.com/place_categories.children"},{"href":"http://api.qype.com/v1/place_categories/2","rel":"http://schemas.qype.com/place_category.parent"}],"id":"tag:api.qype.com,2007-08-23:/places/categories/23",
  15. * "created":"2007-08-23T14:16:38+02:00"}],"links":[{"href":"http://api.qype.com/v1/places/67370","rel":"self"},{"href":"http://www.qype.co.uk/place/67370-Luba-Luft-Hamburg","rel":"alternate","hreflang":"en"},{"href":"http://api.qype.com/v1/places/67370/reviews/de","count":59,"rel":"http://schemas.qype.com/reviews","hreflang":"de"},{"href":"http://api.qype.com/v1/places/67370/reviews/en","count":22,"rel":"http://schemas.qype.com/reviews","hreflang":"en"},{"href":"http://api.qype.com/v1/places/67370/reviews/fr",
  16. * "count":2,"rel":"http://schemas.qype.com/reviews","hreflang":"fr"},{"href":"http://api.qype.com/v1/places/67370/reviews/es","count":1,"rel":"http://schemas.qype.com/reviews","hreflang":"es"},{"href":"http://api.qype.com/v1/places/67370/reviews/pt","count":0,"rel":"http://schemas.qype.com/reviews","hreflang":"pt"},{"href":"http://api.qype.com/v1/places/67370/reviews/pl","count":1,"rel":"http://schemas.qype.com/reviews","hreflang":"pl"},{"href":"http://api.qype.com/v1/places/67370/reviews/it",
  17. * "count":1,"rel":"http://schemas.qype.com/reviews","hreflang":"it"},{"href":"http://api.qype.com/v1/locators/de600-hamburg-st-pauli","rel":"http://schemas.qype.com/locator","title":"St. Pauli"},{"href":"http://api.qype.com/v1/places/67370/tags","rel":"http://schemas.qype.com/tags","hreflang":null},{"href":"http://api.qype.com/v1/places/67370/assets","count":36,"rel":"http://schemas.qype.com/assets"},{"href":"http://api.qype.com/v1/positions/53.5532,9.95799/places?without=67370","rel":"http://schemas.qype.com/places.nearby"},
  18. * {"href":"http://api.qype.com/v1/places/67370/attributes","rel":"http://schemas.qype.com/attributes"},{"href":"http://api.qype.com/v1/places/67370/checkins","count":13,"rel":"http://schemas.qype.com/checkins"},{"href":"http://api.qype.com/v1/places/67370/leaderboard","rel":"http://schemas.qype.com/checkins_rankings"},{"href":"http://api.qype.com/v1/likes?likeable_id=67370\u0026likeable_type=place","count":10,"rel":"http://schemas.qype.com/likes"}],"url":"http://www.Lubaluft.de","phone":"+49 176 48194332",
  19. * "id":"tag:api.qype.com,2007-10-09:/places/67370","created":"2007-10-09T16:18:39+02:00","average_rating":5}};
  20. */
  21. qype.models.PlaceModel = Class.create({
  22. // Initialize model data
  23. initialize: function(data, lang) {
  24. /* ***************************
  25. PLACE OF CODE SNIPPET 12.1
  26. ****************************/
  27. if(!data) {
  28. throw "Initializing PlaceModel without data not allowed!";
  29. return;
  30. }
  31. this.contentLang = lang;
  32. this.data = data;
  33. this.reviews = undefined;
  34. this.categoriesStr = undefined;
  35. },
  36. // Return number of reviews for the place
  37. getReviewsCount: function() {
  38. if(this.data.links && !this.reviews) {
  39. for(var i = 0; i < this.data.links.length; i++) { // Loop through JSON (see the sample data)
  40. var link = this.data.links[i];
  41. if(link.rel == "http://schemas.qype.com/reviews" && link.hreflang == this.contentLang) {
  42. this.reviews = link;
  43. break;
  44. }
  45. }
  46. }
  47. if(this.reviews) {
  48. return this.reviews.count || 0;
  49. }
  50. else {
  51. return 0;
  52. }
  53. },
  54. getReviewsStr: function() {
  55. var count = this.getReviewsCount();
  56. return count + ' ' + ((count == 1) ? qype.str.place_reviews_count_single_postfix : qype.str.place_reviews_count_postfix);
  57. },
  58. getDistanceStr: function() {
  59. return (this.data.distance ?
  60. (Math.round(this.data.distance * 100) / 100) : "-") + " " + qype.str.unit_km;
  61. },
  62. getId: function() {
  63. return this.data.id.substring(this.data.id.lastIndexOf("/") + 1);
  64. },
  65. getCategoriesStr: function() {
  66. if(this.data.categories && !this.categoriesStr) {
  67. var arr = [];
  68. for(var i = 0; i < this.data.categories.length; i++) {
  69. arr.push(this.data.categories[i].title.value);
  70. }
  71. this.categoriesStr = arr.join(", ");
  72. }
  73. return this.categoriesStr || "";
  74. },
  75. // Return image url of the given size
  76. getImageUrl: function(size) {
  77. if(this.data.image) {
  78. return this.data.image[size];
  79. }
  80. return "";
  81. }
  82. });