UNPKG

wise-web-client

Version:

Based on Spine MVC framework

93 lines (79 loc) 3.16 kB
define(['jquery', 'backbone', 'handlebars', './BaseView', 'text!../../../tpl/add_vehicle_modal.hbr' ], function($, Backbone, Handlebars, BaseView, template) { var View = BaseView.extend({ // The DOM Element associated with this view el: '#overlay-container', template: Handlebars.compile(template), // View Event Handlers events: { 'click #addVehicle': 'addVehicle', 'click button.close': 'close' }, // Renders the view's template to the UI render: function() { this.$el.html(this.template()); // Maintains chainability return this; }, addVehicle: function() { var that = this; var vehicleName = $('#avName').val(); var lat = parseFloat($('#avLat').val()); var lng = parseFloat($('#avLng').val()); var type = parseFloat($('#avType').val()); var capacity = parseFloat($('#avCap').val()); var options = { url: this.apiUrl + '/vehicle', data: JSON.stringify({ "name": vehicleName, "location": { "lat": lat, "lng": lng }, "props": { "type": type, "volumeCapacity": capacity, "serviceTime": [{ "start": "8:30:00z", "end": "20:00:00z" }], "hasRefrigerator": false } }), method: 'POST' }; this.talkToServer(options).then( function(res) { var notice = { title: 'A new vehicle is added', text: 'UUID ' + res.data.id }; that.pubsub.trigger('system:notify', notice); that.close(); }, function(res) { var notice = { title: "error", text: res.responseJSON.error }; that.pubsub.trigger('system:notify', notice); that.close(); } ); this.parentView.vehicles.add({ lat: lat, lng: lng, vehicleName: vehicleName, createdTime: "2015-04-29T19:03:04.611Z" }); }, close: function() { $('.modal').modal('hide'); } }); // Returns the View class return View; } );