|
| 1 | +<link rel="import" href="../js/datautils.html"/> |
| 2 | +<link rel="import" href="../js/behaviorutils.html"/> |
| 3 | +<script> |
| 4 | +/** |
| 5 | + * @license |
| 6 | + * Copyright (c) 2015 MediaMath Inc. All rights reserved. |
| 7 | + * This code may only be used under the BSD style license found at http://mediamath.github.io/strand/LICENSE.txt |
| 8 | +
|
| 9 | +*/ |
| 10 | +(function (scope) { |
| 11 | + |
| 12 | + var DataUtils = StrandLib.DataUtils; |
| 13 | + var BehaviorUtils = StrandLib.BehaviorUtils; |
| 14 | + |
| 15 | + function _generateCid(collection, model) { |
| 16 | + model.cId = ++collection._cidIndex; |
| 17 | + } |
| 18 | + |
| 19 | + var _eventPrefix = "data-"; |
| 20 | + |
| 21 | + scope.Collection = { |
| 22 | + |
| 23 | + properties:{ |
| 24 | + _cidIndex:{ |
| 25 | + type:Number, |
| 26 | + value:0 |
| 27 | + }, |
| 28 | + added:{ |
| 29 | + type:Array, |
| 30 | + value: function() { |
| 31 | + return []; |
| 32 | + }, |
| 33 | + notify:true |
| 34 | + }, |
| 35 | + removed:{ |
| 36 | + type:Array, |
| 37 | + value: function() { |
| 38 | + return []; |
| 39 | + }, |
| 40 | + notify:true |
| 41 | + } |
| 42 | + }, |
| 43 | + |
| 44 | + add: function(model, silent, force) { |
| 45 | + this._silent = silent; |
| 46 | + |
| 47 | + if (!model.cId || model.cId === -1) { |
| 48 | + _generateCid(this, model); |
| 49 | + } |
| 50 | + var check = this.data.filter(function(m) { |
| 51 | + if (m.cId && model.cId) { return m.cId === model.cId; } |
| 52 | + if (m.mId && model.mId) { return m.mId === model.mId; } |
| 53 | + return false; |
| 54 | + }); |
| 55 | + if (check.length === 0 || force) { |
| 56 | + this.data.push(model); |
| 57 | + if (!silent) this.fire("changed", {instance:this, data:this.data}); |
| 58 | + return model; |
| 59 | + } else { |
| 60 | + throw(new Error("Model Conflict - matching id's, use force flag to add manually")); |
| 61 | + } |
| 62 | + |
| 63 | + }, |
| 64 | + |
| 65 | + getDataAt: function(index) { |
| 66 | + var m = this.getModelAt(index); |
| 67 | + return DataUtils.getPathValue("data", m) || m; |
| 68 | + }, |
| 69 | + |
| 70 | + getModelAt: function(index) { |
| 71 | + return this.data[index]; |
| 72 | + }, |
| 73 | + |
| 74 | + getIndexOf: function(model) { |
| 75 | + return this.data.indexOf(model); |
| 76 | + }, |
| 77 | + |
| 78 | + delete: function(input) { |
| 79 | + if (input instanceof Model) { |
| 80 | + this.data.slice(this.data.indexOf(input), 1); |
| 81 | + } else { |
| 82 | + var d = this.data.slice(input, 1); |
| 83 | + if (!d) { |
| 84 | + this.data.filter(function(model) { |
| 85 | + return model.mid !== input && model.cId !== input; |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + }, |
| 90 | + |
| 91 | + empty: function(silent) { |
| 92 | + this._silent = silent; |
| 93 | + this._cidIndex = 0; |
| 94 | + var o = this.data.slice(); |
| 95 | + this.set("data",[]); |
| 96 | + return o; |
| 97 | + }, |
| 98 | + |
| 99 | + each: function(callback) { |
| 100 | + if(this.data && this.data.length) { |
| 101 | + this.data.forEach(callback, this); |
| 102 | + } |
| 103 | + }, |
| 104 | + |
| 105 | + where: function(obj, matchValues) { |
| 106 | + if (this.data && this.data.length) { |
| 107 | + var objKeys = Object.keys(obj); |
| 108 | + return this.data.filter(function match(model) { |
| 109 | + var data = model.data || model; |
| 110 | + return objKeys.reduce(function(prev, key) { |
| 111 | + return prev && (matchValues ? data[key] === obj[key] : data.hasOwnProperty(key)); |
| 112 | + }, true); |
| 113 | + }); |
| 114 | + } |
| 115 | + }, |
| 116 | + |
| 117 | + // _handleResponse: function(e) { |
| 118 | + // this.data = DataUtils.getPathValue("detail.marshalled",e) || DataUtils.getPathValue("detail.response",e); |
| 119 | + // this.fire("changed", {instance:this, data:this.data}); |
| 120 | + // }, |
| 121 | + |
| 122 | + get length() { |
| 123 | + return this.data.length; |
| 124 | + }, |
| 125 | + |
| 126 | + get count() { |
| 127 | + return this._count || this.length; |
| 128 | + }, |
| 129 | + |
| 130 | + toJSON: function() { |
| 131 | + this.data.toJSON(); |
| 132 | + } |
| 133 | + }; |
| 134 | + |
| 135 | +})(window.StrandTraits = window.StrandTraits || {}); |
| 136 | +</script> |
0 commit comments