UNPKG

lpio

Version:

The last dashboard app you'll ever need

67 lines (60 loc) 2.34 kB
<link rel="import" href="../../../bower_components/polymer/polymer.html"> <link rel="import" href="crud-api.html"> <script> window.ApiBehaviors = window.ApiBehaviors || {}; /** * * @polymerBehavior ApiBehaviors.Widget * **/ ApiBehaviors.WidgetImpl = { createWidget: function(widget){ var self = this; // @TODO: add validation if(!_.isObject(widget)){ console.error("createWidget: invalid object"); return; } self.setSaving('widgets'); self.createEntity('widget',widget) .then(function(response){ console.log("Created widget with id:"+response.data.id); // added a new widget, refresh widgets if(self.state.app.currentDashboard != ''){ self.getWidgetsByDashboardId(self.state.app.currentDashboard); } }) .catch(function(err){ console.error(err); self.setError('Error creating new widget'); }) .finally(function(){ self.setSavingDone('widgets'); }).done(); }, getWidgetsByDashboardId: function(dashboard_id){ var self = this; if(!dashboard_id || dashboard_id.length < 5){ console.error("getWidgetsByDashboardId: dashboard_id is required"); return; } var url = "http://"+self.host + ":" + self.port + "/v1/dashboard/"+dashboard_id; self.setLoading('widgets'); $.ajax({ url: url, contentType: "application/json" }) .done(function(data) { self.dispatch({key:'widgets',type:'setByDashboardId', widgets:data, dashboardId:dashboard_id}); }) .fail(function(jqXHR, textStatus) { console.error(textStatus); self.setError('Error retrieving widgets by Dashboard Id: '+dashboard_id); }) .always(function(){ self.setLoadingDone('widgets'); }); } }; ApiBehaviors.Widget = [ApiBehaviors.Crud,ApiBehaviors.WidgetImpl]; </script>