-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.template-data.js
64 lines (49 loc) · 2.25 KB
/
backbone.template-data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(function(){
// add .templateData() method to all Models
Backbone.Model.prototype.templateData = function(){ return this.toTemplateData() } // ! DEPRECATED
Backbone.Model.prototype.toTemplateData = function(){
var that = this;
var data = this.toJSON();
// for each attribute, look for models and collections to convert
_.each(this.attributes, function(attr, key){
if( that.attrTypes && that.attrTypes[key] && that.attrTypes[key] != 'moment' ) // NOTE: not sure I want to always exclude moment types
data[key] = that.get(key)
// is this attribute a Model?
else if( attr instanceof Backbone.Model && attr.displayVal )
data[key] = attr.displayVal();
// is it a collection?
else if( attr instanceof Backbone.Collection )
data[key] = attr.toTemplateData();
})
// support for ChildCollection models
if( this.models )
_.each(this.models, function(d, key){
if( d.template )
data[key] = that.get(key) ? that.get(key).toTemplateData() : null
})
if( this._templateData )
_.each(this._templateData, function(val, key){
if( val === '' ) val = _.camelize(key);
data[key] = _.isFunction(val) ? val.call(that, val) : (that[val] && _.isFunction(that[val]) ? that[val].call(that) : that.get(val));
if( data[key] instanceof Backbone.Model )
data[key] = data[key].templateData()
})
return data;
}
Backbone.Collection.prototype.templateData = function(){ return this.toTemplateData() } // ! DEPRECATED
Backbone.Collection.prototype.toTemplateData = function(){return this.map(function(m){return m.templateData()})}
Backbone.Collection.prototype.toSelectID = function(labelKey, valKey, extraData){
var isString = _.isString(labelKey);
return this.map(function(m){
var d = {
'label':labelKey?(isString?(m[labelKey]&&m[labelKey].call(m))||m.get(labelKey):labelKey(m)):m.id,
'val':(valKey?m.get(valKey):m.id)
}
if( extraData ) _.each(extraData, function(val, key){
if( val === '' ) val = key;
d[key] = _.isFunction(val) ? val(m) : ((m[val]&&m[val].call(m))||m.get(val))
})
return d
})
}
})()