npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone-serialize

v1.1.0

Published

Serialize Backbone models and collections into JSON representations

Downloads

8

Readme

backbone-serialize Build status

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.

Getting Started

npm

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'}]

bower

Install the module with: bower install backbone-serialize

<script src="bower_components/backbone-serialize/dist/backbone-serialize.min.js"></script>
<script>
  window.BackboneSerialize; // Use same as in `npm`
</script>

Vanilla

Download the minified JS at:

https://raw.githubusercontent.com/underdogio/backbone-serialize/master/dist/backbone-serialize.min.js

<script src="backbone-serialize.min.js"></script>
<script>
  window.BackboneSerialize; // Use same as in `npm`
</script>

Documentation

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.

  • Backbone Object - Backbone library to mix into
    • Model Function - Constructor for Backbone models
    • Collection Function - Constructor for Backbone collections

Returns:

  • Backbone 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.

  • dynamicAttributes String[] - Array of strings for methods to run and save as serialized attributes
    • For example, if dynamicAttributes is ['formatted_date'], then model.formatted_date() would be run and saved to the serialized result

Usage 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:

  • retObj Object - Serialized form of model.attributes
    • This will be key/value pairs that directly correspond to model.attributes
    • If any of the attributes were a model or collection, then this will be their .serialize()'d form
    • If model.dynamicAttributes was defined, then those properties will be executed and be preset on retObj under their respective keys

collection.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:

  • retArr Object[] - Array of serialized form of collection.models
    • This will be in the same order as collection.models
    • If any of the models were a model, then this will be their .serialize()'d form

Contributing

In 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.

License

Copyright (c) 2015 Underdog.io

Licensed under the MIT license.