var Qs_Lock = qs.createObject(); Qs_Lock.prototype = { _resourceName: null, _objectId: null, _refreshTimeout: 60 * 1000, _url: null, _timestamp: 0, _timer: null, initialize: function (options) { if (typeof options != 'object') { alert('Lock Object Data is wrong'); return false; } if (!this._setResourceName(options.resourceName)) { return false; } if (!this._setObjectId(options.objectId)) { return false; } if (typeof options.url != 'undefined' && !this._setUrl(options.url)) { return false; } if (typeof options.refreshTimeout != 'undefined' && !this._setRefreshTimeout(options.refreshTimeout)) { return false; } var d = new Date(); this._timestamp = d.getTime(); this.start(); }, _setResourceName: function (name) { if (!_.isString(name) || '' == _.str.trim(name)) { alert('Error: Resource name is not defined'); return false; } this._resourceName = _.str.trim(name); return true; }, _setObjectId: function (objectId) { if (_.isNaN(objectId) || 0 == objectId) { alert('Error: Object ID is not defined'); return false; } this._objectId = parseInt(objectId); return true; }, _validateUrl: function (url) { if (!_.isString(url) || '' == _.str.trim(url)) { alert('Error: Lock Refresh URL is not defined'); return false; } return true; }, _setUrl: function (url) { if (!this._validateUrl(url)) { return false; } this._url = _.str.trim(url); return true; }, _setRefreshTimeout: function (timeout) { if (_.isNaN(timeout) || 0 == timeout) { alert('Error: Refresh timeout interval is wrong!'); return false; } this._refreshTimeout = parseInt(timeout) * 1000; return true; }, _sendRequest: function () { if (!this._validateUrl(this._url)) { return false; } $.ajax({ url: this._url + '?' + this._timestamp + '&resourceName=' + this._resourceName + '&objectId=' + this._objectId, type: 'GET', dataType: 'json' }).done(_.bind(this._requestOnDone, this)); this._timestamp++; return true; }, _requestOnDone: function (data, textStatus, jqXHR) { if (textStatus == 'success' && data.status == true) { this.start(); } }, start: function () { if (!this._validateUrl(this._url)) { return false; } this.stop(); this._timer = setTimeout(_.bind(this._sendRequest, this), this._refreshTimeout); return true; }, stop: function () { if (this._timer != null) { clearTimeout(this._timer); } this._timer = null; return true; } };