Skip to content

Commit 802af9a

Browse files
author
Dan Lasky
committed
moving the object implementations to behaviors
1 parent 2042fb9 commit 802af9a

11 files changed

Lines changed: 235 additions & 425 deletions

File tree

src/mm-collection/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<body>
88
<mm-collection></mm-collection>
99
<script type="text/javascript">
10-
window.addEventListener("polymer-ready", function() {
10+
window.addEventListener("WebComponentsReady", function() {
1111
document.querySelector("mm-collection").addEventListener("data-changed", function(e) {
1212
console.log("test handler - data changed", e);
1313
});
1414
});
1515
</script>
1616
</body>
17-
</html>
17+
</html>

src/mm-collection/mm-collection.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-->
77
<link rel="import" href="../../bower_components/polymer/polymer.html"/>
88
<link rel="import" href="../shared/js/datautils.html"/>
9-
<link rel="import" href="../shared/js/collection.html"/>
9+
<link rel="import" href="../shared/behaviors/collection.html"/>
1010
<link rel="import" href="../shared/behaviors/pageable.html"/>
1111
<link rel="import" href="../shared/behaviors/refable.html"/>
1212

@@ -16,4 +16,4 @@
1616
<content id="content"></content>
1717
</template>
1818
<script src="mm-collection.js"></script>
19-
</dom-module>
19+
</dom-module>

src/mm-collection/mm-collection.js

Lines changed: 2 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -18,118 +18,15 @@
1818

1919
properties: {
2020

21-
// adapter:{
22-
// type:String,
23-
// value:"Sync",
24-
// observer:"_adapterChanged"
25-
// }
2621
},
2722

2823
behaviors: [
29-
StrandLib.Collection.getBehavior(),
24+
// StrandLib.Sync.getBehavior(), //interhited from pageable
25+
StrandTraits.Collection,
3026
StrandTraits.Pageable,
3127
StrandTraits.Refable
3228
],
3329

34-
// factoryImpl: function(auto) {
35-
// this.auto = auto;
36-
// },
37-
//
38-
// ready: function() {
39-
// this._collection = new StrandLib.Collection(null, null, this._sync);
40-
// },
41-
//
42-
// create: function(init, silent) {
43-
// return this._collection.create(init, silent);
44-
// },
45-
//
46-
// add: function(model, silent, force) {
47-
// return this._collection.add(model, silent, force);
48-
// },
49-
//
50-
// getDataAt: function(index) {
51-
// return this._collection.getDataAt(index);
52-
// },
53-
//
54-
// getModelAt: function(index) {
55-
// return this._collection.getModelAt(index);
56-
// },
57-
//
58-
// delete: function(input) {
59-
// this._collection.delete(input);
60-
// },
61-
//
62-
// clear: function(silent) {
63-
// this._collection.empty(silent);
64-
// },
65-
//
66-
// empty: function(silent) {
67-
// this._collection.empty(silent);
68-
// },
69-
//
70-
// save: function() {
71-
// return this.connection.post();
72-
// },
73-
//
74-
// update: function() {
75-
// return this.connection.put();
76-
// },
77-
//
78-
// fetch: function() {
79-
// return this.connection.get();
80-
// },
81-
//
82-
// destroy: function() {
83-
// return this.connection.delete();
84-
// },
85-
//
86-
// each: function(callback) {
87-
// this._collection.each(callback);
88-
// },
89-
//
90-
// where: function(obj, matchValues) {
91-
// return this._collection.where(obj,matchValues);
92-
// },
93-
//
94-
// _adapterChanged: function() {
95-
// //TODO: (dlasky) update to new adapters
96-
// },
97-
//
98-
// // _syncChanged: function() {
99-
// // this._collection._sync = this._sync;
100-
// // },
101-
//
102-
// // _collectionChanged: function() {
103-
// // this._collection.addEventListener("data-changed", function() {
104-
// // this.fire("data-changed");
105-
// // }.bind(this));
106-
// // },
107-
//
108-
// get data() {
109-
// return DataUtils.getPathValue("_collection.data", this);
110-
//
111-
// },
112-
//
113-
// set data(input) {
114-
// DataUtils.setPathValue("_collection.data", this, input);
115-
// //TODO: evented
116-
// },
117-
//
118-
// get connection() {
119-
// return this._collection.connection;
120-
// },
121-
//
122-
// get length() {
123-
// return this._collection.length;
124-
// },
125-
//
126-
// get count() {
127-
// return this._collection.count;
128-
// },
129-
//
130-
// toJSON: function() {
131-
// this._collection.toJSON();
132-
// }
13330

13431
});
13532
})(window.Strand = window.Strand || {});

src/mm-model/mm-model.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
66
-->
77
<link rel="import" href="../../bower_components/polymer/polymer.html"/>
8-
<link rel="import" href="../shared/js/model.html"/>
8+
<link rel="import" href="../shared/js/sync.html"/>
9+
<link rel="import" href="../shared/behaviors/model.html"/>
910
<link rel="import" href="../shared/behaviors/domsyncable.html"/>
1011
<link rel="import" href="../shared/behaviors/refable.html"/>
1112

@@ -15,4 +16,4 @@
1516
<content id="content"></content>
1617
</template>
1718
<script src="mm-model.js"></script>
18-
</dom-module>
19+
</dom-module>

src/mm-model/mm-model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
behaviors: [
2020
StrandLib.Sync.getBehavior(),
21-
StrandLib.Model.getBehavior(),
21+
StrandTraits.Model,
2222
StrandTraits.DomSyncable,
2323
StrandTraits.Refable
2424
],
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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>

src/shared/behaviors/model.html

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<link rel="import" href="../js/datautils.html"/>
2+
<link rel="import" href="../js/behaviorutils.html"/>
3+
<link rel="import" href="../js/sync.html"/>
4+
<link rel="import" href="../js/eventdispatcher.html"/>
5+
<script>
6+
/**
7+
* @license
8+
* Copyright (c) 2015 MediaMath Inc. All rights reserved.
9+
* This code may only be used under the BSD style license found at http://mediamath.github.io/strand/LICENSE.txt
10+
11+
*/
12+
(function (scope) {
13+
14+
var DataUtils = StrandLib.DataUtils;
15+
var BehaviorUtils = StrandLib.BehaviorUtils;
16+
var _eventPrefix = "data-";
17+
18+
scope.Model = {
19+
20+
get mId() {
21+
return this.data.id || null;
22+
},
23+
24+
set mId (input) {
25+
this.set("data.id", input);
26+
// this.data.id = input;
27+
},
28+
29+
get cId() {
30+
return this._cid;
31+
},
32+
33+
set cId(input) {
34+
this._cid = input;
35+
},
36+
37+
init: function(data) {
38+
// this.data = DataUtils.copy({}, this.data, data);
39+
// this.fire("changed", {instance: this, data:this.data});
40+
this.set("data", data);
41+
},
42+
43+
clear: function() {
44+
this.data = {};
45+
this.fire("changed", {instance: this, data:this.data});
46+
},
47+
48+
getProp: function(path) {
49+
return DataUtils.getPathValue(path, this.data);
50+
},
51+
52+
setProp: function(path, value) {
53+
// this.dispatchEvent(new CustomEvent("data-changed"));
54+
// DataUtils.setPathValue(path, this.data, value);
55+
this.set("data." + path, value);
56+
},
57+
58+
toJSON: function() {
59+
return JSON.stringify(this.data);
60+
},
61+
62+
// _handleResponse: function(e) {
63+
// this.data = DataUtils.getPathValue("detail.marshalled",e) || DataUtils.getPathValue("detail.response",e);
64+
// this.fire("changed", {instance:this, data:this.data});
65+
// },
66+
67+
};
68+
69+
})(window.StrandTraits = window.StrandTraits || {});
70+
</script>

0 commit comments

Comments
 (0)