
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
backbone-attribute-types
Advanced tools
A non-obtrusive plugin that extends models to force `get()`ing of attributes to return values in a specific type (integer, date, etc).
A non-obtrusive plugin that extends models to force
get()ing of attributes to return values in a specific type (integer, date, etc).
var TypedModel = Backbone.Model.extend({
attrTypes: {
'count': 'int',
'runtime': 'float',
'created_at': 'moment'
}
});
var typedModel = new TypedModel({
count: '10',
runtime: '4.75',
created_at: '2017-09-19'
})
typedModel.get('count') // 10
typedModel.get('runtime') // 4.75
typedModel.get('created_at') // date object
// a use-case
typedModel.attributes['count'] + 9 // = "109" (this would normally happen)
typedModel.get('count') + 9 // = 19
attrTypesThere are default attribute types under Backbone.ModelAttrTypes. You can add to this or specify a custom type on the model.
attrTypes can also be defined on the Collection.
window.Backbone.ModelAttrTypes = {
'string': function(val){ return String(val) },
'json': function(val){ return _.isString(val) ? JSON.parse(val) : val },
'bool': function(val){ return !!val },
'int': function(val){ return parseInt(val) },
'float': function(val){ return parseFloat(val) },
'num': function(val){ return parseFloat(val) }, // alias for float
'date': function(val){ return new Date(val) },
// moment.js support
'moment': function(val){ return window.moment ? moment(val) : new Date(val) }
}
var CustomTypedModel = Backbone.Model.extend({
attrTypes: {
'attrName': function(val){
// convert val to type
return val
}
}
});
You can cast an attribute to a type on the fly by appending a pipe | and the type at the end of the attribute name.
typedModel.get('runtime') // 4.75
typedModel.get('runtime|int') // 4
typedModel.get('runtime|string') // "4.75"
You can also cast as raw to get the original value stored in attributes
typedModel.get('runtime|raw') // "4.75"
The reason for this plugin is for when your model has an attribute in a string format (for JSON and database consistency) but you wish to use the value in code as a certain type. Rather than 1) continually converting the value to your desired type or 2) creating a secondary method to make this conversion, this plugin will do it for you.
It's designed to be non-obtrusive by refraining from overwriting the real value in attributes and allows you to continue using .get(attr) like normal.
v0.1.3
v0.1.2
MIT © Kevin Jantzer
FAQs
A non-obtrusive plugin that extends models to force `get()`ing of attributes to return values in a specific type (integer, date, etc).
We found that backbone-attribute-types demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.