-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.attribute-types.js
71 lines (53 loc) · 2.42 KB
/
backbone.attribute-types.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
65
66
67
68
69
70
71
/*
Backbone Model Attributes Types 0.1.1
A non-obtrusive plugin to force `get()`ing of attributes to return values in
a specific type (integer, date, etc). It's designed to be used in code and refrains
from overwriting the real value in `attributes`.
@author Kevin Jantzer, Blackstone Audio
@since 2017-09-19
@TODO:
- should null/undefined value parsing be more robust?
*/
(function(){
var GetAttr = Backbone.Model.prototype.get
// global so it can be added to
window.Backbone.ModelAttrTypes = {
'string': function(val){ return String(val) },
'bool': function(val){ return !!val },
'int': function(val){ return parseInt(val) },
'float': function(val){ return parseFloat(val) },
'num': function(val){ return parseFloat(val) },
'date': function(val){ return new Date(val) },
'moment': function(val){ return window.moment ? moment(val) : new Date(val) },
'moment!': function(val){ return window.moment ? moment(val) : new Date(val) } // hack for now (see backbone.template-data)
}
function convertToType(attr, type, val){
// set storage properties
this.__attributesRaw = this.__attributesRaw || {}
this.__attributes = this.__attributes || {}
var key = attr+type
// current value is different then the last time we got it (or we've never got it before), so convert the value to type now
if( val != this.__attributesRaw[key] || this.__attributes[key] === undefined ){
this.__attributesRaw[key] = val // remember the raw value we converted from so we know whether to change it again
// custom functions are supported
if( _.isFunction(type) )
val = type(val)
else if( Backbone.ModelAttrTypes[type] )
val = Backbone.ModelAttrTypes[type].call(this, val)
else
console.warn('`'+type+'` is not a supported attribute type')
// cache the converted value
this.__attributes[key] = val
}
return this.__attributes[key]
}
Backbone.Model.prototype.get = function(attr){
var [attr, type] = attr.split('|') // check to see if user is casting: `attrName|castType`
var val = GetAttr.call(this, attr) // raw value
var type = type || (this.attrTypes && this.attrTypes[attr]) || (this.collection && this.collection.attrTypes && this.collection.attrTypes[attr]) // the type the value should be
// no types given, or no type for this attribute (or no value for attr), so return the value as is
if( !type || !val || type == 'raw')
return val
return convertToType.call(this, attr, type, val)
}
})()