-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverson-traversal.html
1 lines (1 loc) · 2.96 KB
/
traverson-traversal.html
1
<link rel="import" href="../polymer/polymer.html"> <script src="../traverson/browser/dist/traverson.min.js"></script> <script src="../traverson-hal/browser/dist/traverson-hal.min.js"></script> <script>"use strict";traverson.registerMediaType(TraversonJsonHalAdapter.mediaType,TraversonJsonHalAdapter),Polymer({is:"traverson-traversal",hostAttributes:{hidden:!0},properties:{traverson:{type:Object,notify:!0,readonly:!0,value:traverson},from:{type:String},follow:{type:Array,value:null},templateParameters:{type:Object,value:null},requestOptions:{type:Object,value:null},handleAs:{type:String,value:"json"},method:{type:String,value:"GET"},body:{type:Object,value:null},auto:{type:Boolean,value:!1},lastRequest:{type:Object,notify:!0,readOnly:!0},lastTraversal:{type:Object,notify:!0,readOnly:!0},loading:{type:Boolean,notify:!0,readOnly:!0},lastResponse:{type:Object,notify:!0,readOnly:!0},lastError:{type:Object,notify:!0,readOnly:!0},activeTraversals:{type:Array,notify:!0,readOnly:!0,value:function(){return[]}},debounceDuration:{type:Number,value:0,notify:!0},bubbles:{type:Boolean,value:!1},_boundHandleResponse:{type:Function,value:function(){return this._handleResponse.bind(this)}}},observers:["_traversalOptionsChanged(from, follow.*, templateParameters.*, requestOptions, body, handleAs, method, auto)"],generateTraversal:function(){var e=this.traverson.from(this.from);switch(this.handleAs){case"json":e=e.json();break;case"jsonHal":e=e.jsonHal();break;default:e=e.setMediaType(this.handleAs)}this.requestOptions&&(e=e.withRequestOptions(this.requestOptions)),this.follow&&(e=e.follow(this.follow)),this.templateParameters&&(e=e.withTemplateParameters(this.templateParameters));var t;switch(this.method){case"GET":t=e.getResource(this._boundHandleResponse.bind(this,e));break;case"PUT":t=e.put(this.body,this._boundHandleResponse.bind(this,e));break;case"PATCH":t=e.patch(this.body,this._boundHandleResponse.bind(this,e));break;case"POST":t=e.post(this.body,this._boundHandleResponse.bind(this,e));break;case"DELETE":t=e["delete"](this._boundHandleResponse.bind(this,e))}return this.push("activeTraversals",t),this._setLastTraversal(t),this._setLastRequest(e),this._setLoading(!0),this.fire("traversal",{request:e,traversal:t},{bubbles:this.bubbles}),t},_handleError:function(e,t,s,a){e===this.lastRequest&&(this._setLastError({request:e,traversal:a,error:t,response:s}),this._setLastTraversal(a),this._setLastResponse(null),this._setLoading(!1))},_handleResponse:function(e,t,s,a){return t?void this._handleError(a,t,s,a):(e===this.lastRequest&&(this._setLastTraversal(a),this._setLastResponse(s),this._setLastError(null),this._setLoading(!1)),void this.fire("response",{request:e,traversal:a,response:s},{bubbles:this.bubbles}))},_discardTraversal:function(e){var t=this.activeTraversals.indexOf(e);t>-1&&this.splice("activeTraversals",t,1)},_traversalOptionsChanged:function(){this.debounce("generate-traversal",function(){null!==this.from&&this.auto&&this.generateTraversal()},this.debounceDuration)}})</script>