fusionjs
Version:
FusionJS is a JS toolkit that provides library/framework agnostic tools
1 lines • 2.21 kB
JavaScript
"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}();Object.defineProperty(exports,"__esModule",{value:!0});var FusionStore=function(){function e(t){if(_classCallCheck(this,e),this.path="","undefined"==typeof t.model)throw new Error("Model property is required");this._records=[],this.model=t.model,t.path&&(this.path=t.path),t.data&&this.set(t.data)}return _createClass(e,[{key:"toObject",value:function(){var e=[];return this._records.forEach(function(t){e.push(t.toObject())}),e}},{key:"set",value:function(e){var t=this;this._recordsIndex=this._recordsIndex||{},e instanceof Array?e.forEach(function(e){t._setRecord(e)},this):this._setRecord(e)}},{key:"setData",value:function(e){this.reset(),this.set(e)}},{key:"get",value:function(e){return this._getRecords(e)}},{key:"_setRecord",value:function(e){var t=void 0,r=void 0,n=this.model,s=void 0;e instanceof n?s=e.get():(s=Object.assign({},e),e=(new n).init()),t=s[e.idProperty],t=t?t:this._records.length+1,r=this._recordsIndex[t],"undefined"==typeof r&&(r=this._records.push(e)-1,this._recordsIndex[t]=r,e._setPath(this.path+"$"+r)),this._records[r].set(s)}},{key:"_getRecords",value:function(e){return e?this._records&&this._recordsIndex&&this._records[this._recordsIndex[e]]||null:this._records}},{key:"reset",value:function(){this._records=[],this._recordsIndex=null}},{key:"query",value:function(e){console.warn("query method is experimental and not yet fully supported, and is likely to change!");var t=[],r=this.get();return r.forEach(function(r){var n=r.query(e);t=t.concat(n)}),t}},{key:"equals",value:function(e){return this._isEqual(e)}},{key:"strictEquals",value:function(e){var t=this===e;return t||this._isEqual(e)}},{key:"_isEqual",value:function(e){var t=this.get(),r=e.get(),n=t.length===r.length;return n&&t.some(function(e,t){var s=r[t];return n=e.equals(s),!n}),n}}]),e}();exports.FusionStore=FusionStore;