Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
backbone-serialize
Advanced tools
Serialize Backbone models and collections into JSON representations
This was written to prevent mutating model/collection state once inside of a template. This was heavily inspired by Chaplin's serialize method.
Install the module with: npm install backbone-serialize
// Add our bindings to Backbone
var BackboneSerialize = require('backbone-serialize');
var Backbone = BackboneSerialize.mixin(require('backbone'));
// Create a model and serialize it
var model = new Backbone.Model({
hello: 'world'
});
model.serialize(); // {hello: 'world'}
// Create a model with a model and serialize it
var model = new Backbone.Model({
name: 'Earth',
galaxy: new Backbone.Model({
name: 'Milky Way'
})
});
model.serialize(); // {name: 'Earth', galaxy: {name: 'Milky Way'}}
// Create a model with dynamic attributes
var PersonModel = Backbone.Model.extend({
dynamicAttributes: ['full_name'],
full_name: function () {
return this.get('first_name') + ' ' + this.get('last_name');
}
});
var person = new PersonModel({
first_name: 'Bark',
last_name: 'Ruffalo'
});
person.serialize(); // {first_name: 'Bark', full_name: 'Bark Ruffalo', last_name: 'Ruffalo'}
// Create a collection and serialize it
var collection = new Backbone.Collection([
new Backbone.Model({
word: 'hello'
}),
new Backbone.Model({
word: 'world'
})
]);
collection.serialize(); // [{word: 'hello'}, {word: 'world'}]
backbone-serialize
exposes the function mixin
via exports.mixin
.
mixin(Backbone)
Add serialize
bindings to Backbone
library
This function mutates Backbone.Model.prototype
and Backbone.Collection.prototype
and adds a serialize
method to each.
Object
- Backbone library to mix into
Function
- Constructor for Backbone modelsFunction
- Constructor for Backbone collectionsReturns:
Object
- Original Backbone
library passed in with mutated Backbone.Model
and Backbone.Collection
model.dynamicAttributes
Array of attributes that should be added to the returned serialized values. It is preferred to define this via Model.extend
but it can be defined directly on instances.
String[]
- Array of strings for methods to run and save as serialized attributes
dynamicAttributes
is ['formatted_date']
, then model.formatted_date()
would be run and saved to the serialized resultUsage via Model.extend:
var UserModel = Backbone.Model.extend({
dynamicAttributes: ['formatted_date'],
formatted_date: function () {
// `last_login_date` is a Date, we are formatting it to a human friendlier string
// e.g. 'Wed Feb 25 2015'
return this.get('last_login_date').toDateString();
}
});
var user = new UserModel({
username: 'barkruffalo'
});
user.serialize(); /*
{
formatted_date: 'Wed Feb 25 2015',
username: 'barkruffalo'
}
*/
model.serialize()
Return JSON form of model.attributes
. This iterates over model.attributes
, serializes any models/collections, and saves all attributes into an object.
Additionally, it supports model.dynamicAttributes
which allows for converting methods into their serialized form.
Returns:
Object
- Serialized form of model.attributes
model.attributes
.serialize()'d
formmodel.dynamicAttributes
was defined, then those properties will be executed and be preset on retObj
under their respective keyscollection.serialize()
Returns JSON form of collection.models
. This iterates over collection.models
, serializes any models, and saves all results into an array of objects.
Returns:
Object[]
- Array of serialized form of collection.models
collection.models
.serialize()'d
formIn lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via npm run lint
and test via npm test
.
Copyright (c) 2015 Underdog.io
Licensed under the MIT license.
FAQs
Serialize Backbone models and collections into JSON representations
The npm package backbone-serialize receives a total of 2 weekly downloads. As such, backbone-serialize popularity was classified as not popular.
We found that backbone-serialize 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.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.