UNPKG

waterloo-lookup-fetcher

Version:

Lookup fetcher

154 lines (120 loc) 4.29 kB
import { Mapper } from 'waterloo-mapper'; let LookupFetcher = function (vueInstance) { let self = this; this.idPropName = 'id'; this.rootAttributeName = 'lookups' this.vueInstance = vueInstance; this.name = 'Lookup'; this.fields = ['id', 'name']; this.apiEndpoint = ''; this.urlParams = null; this.failureCallback = null; this.afterFetchCallback = null; this.mapper = new Mapper(); this.isCustomMapper = false; }; // Setters LookupFetcher.prototype.Name = function(name) { this.name = name; return this; }; LookupFetcher.prototype.SetMapper = function(mapper) { this.mapper = mapper; this.isCustomMapper = true; return this; }; LookupFetcher.prototype.IDProp = function(idPropName) { this.idPropName = idPropName; return this; }; LookupFetcher.prototype.RootName = function(rootAttributeName) { this.rootAttributeName = rootAttributeName; return this; }; LookupFetcher.prototype.Fields = function(fields) { this.fields = fields; // if custom mapping is applied we prevent the default one here if(this.isCustomMapper){ return this; } var self = this; fields.forEach(function(i) { self.mapper.for(i).use(function(src) { return src[i];}); }); return this; }; LookupFetcher.prototype.AddField = function (field) { this.fields.push(field); // if custom mapping is applied we prevent the default one here if(this.isCustomMapper){ return this; } this.mapper.for(field).use(function(src) { return src[field];}); return this; }; /** * The respond from the server must be either an array with the data itself or and object with property 'data' containing the array * @param {api endpoint you are calling} apiEndpoint */ LookupFetcher.prototype.Api = function(apiEndpoint, urlParams) { this.apiEndpoint = apiEndpoint; this.urlParams = urlParams; return this; }; LookupFetcher.prototype.FailureCallback = function(callback) { this.failureCallback = callback; return this; }; /** * Attach your callback function which will be called after the lookup is fetched and populated * The callback function will receive an array with the data */ LookupFetcher.prototype.AfterFetch = function(callback) { this.afterFetchCallback = callback; return this; } // Methods LookupFetcher.prototype.Fetch = function() { if(!this.vueInstance[this.rootAttributeName]){ this.vueInstance[this.rootAttributeName] = {}; } this.vueInstance.$http.get(this.vueInstance.$rootApiPath + this.apiEndpoint, { params: this.urlParams }) .then(this.Read.bind(this), this.ReadFailure.bind(this)); this.vueInstance.$set(this.vueInstance[this.rootAttributeName], this.Name, []) return this; }; LookupFetcher.prototype.ReadFailure = function(event) { if(this.failureCallback){ this.failureCallback(event); } } LookupFetcher.prototype.Read = function(event) { this.vueInstance[this.rootAttributeName][this.name] = []; var data = event.body.data ? event.body.data : event.body; for (var i in data) { var obj = {}; this.fields.forEach(function(i) { obj[i] = null; }); this.mapper.from(data[i]).to(obj).map(); this.vueInstance[this.rootAttributeName][this.name].push(obj); } this.vueInstance.$forceUpdate(); // Call AfterFetch if (this.afterFetchCallback) { this.afterFetchCallback(this.vueInstance[this.rootAttributeName][this.name]); } return this; }; // Non chained methods returning the final object/s LookupFetcher.prototype.GetAll = function(id) { return this.vueInstance[this.rootAttributeName][this.name]; }; LookupFetcher.prototype.Wait = function () { return this.vueInstance[this.rootAttributeName][this.name]; }; LookupFetcher.prototype.Find = function (id) { if (this.vueInstance[this.rootAttributeName][this.name]) { return this.vueInstance[this.rootAttributeName][this.name].find(function(item) { item[this.idPropName] === id; }); } return ''; }; export { LookupFetcher }