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-jsonify

v1.0.0

Published

Backbone helper that alters "toJson" function to extend functionalities.

Downloads

2

Readme

backbone-jsonify

Backbone Jsonify alters and provides a extended configuration to the "toJSON" method for models and collections. In general terms, the library enhances it by the following features:

  • Serializes deeply. This means model's attributes which are arrays and objects will be copied deeply.
  • Serializes model and collection. When model's attributes are another model or a collection, those will be serialized as well.
  • Filters output. "toJSON" method accepts new configuration options in order to control its output. This means to be useful for persisting and templating processes, when you wish to filter somehow data you want to work with.

Usage

The new configuration options accepted by "toJSON" function are described below:

model.toJSON([options]) 

Options

pick

String, String[], Boolean, or Function. Returns the object which represents the model but only picks the provided attribute/s.

The function looks through each attribute in the model, and picks all the attributes that pass a truth test defined by the function, which is invoked with up to three arguments; (value [, index|key, object]).

omit

String, String[], Boolean or Function. Returns the object which represents the model but omits the provided attribute/s.

The function looks through each attribute in the model, and omits all the attributes that pass a truth test defined by the function, which is invoked with up to three arguments; (value [, index|key, object]).

deepJson

Boolean. Whether or not to copy deeply model's attributes which are array or objects.

Examples

var artist = new Backbone.Model({
  firstName: "Wassily",
  lastName: "Kandinsky"
});

artist.toJSON({
	pick: "firstName" // Only picks firstName
}); // Outputs {firstName: "Wassily"}

artist.toJSON({
	omit: "firstName" // Omits firstName
}); // Outputs {lastName: "Kandinsky"}

artist.set({age: 26});

artist.toJSON({
	pick: function(attrKey, attrValue) { // Only picks firstName and attribute values that are numbers
        return (attrKey == "firstName") || _.isNumber(attrValue);
    }
}); // Outputs {firstName: "Wassily", age: 26}

artist.toJSON({
	omit: function(attrKey, attrValue) { // Omits firstName and attribute values that are numbers
        return (attrKey == "firstName") || _.isNumber(attrValue);
    }
}); // Outputs {lastName: "Kandinsky"}

Supermodel compatibility

Supermodel is a Backbone plugin for model tracking and relationships between models. Backbone.Jsonify has been adapted to work along such library and it provides more options to address the jsonify process of the relationships.

For using it, it is needed to include the Supermodel library just before supermodel.jsonify.js or the minimized version of the plugin.

Options

assoc

Serialices the model associations by a configuration.

Object representing:

{
	'*': defaultConfiguration, // Takes a default configuration for all associations (optional)
	associationName : configuration,
    // [...] more association configs
}

The Configuration may accept a boolean or an object.

  • Boolean. Set to true to serialize the related model and the full set of attributes and associations. In case of false, the relationship is not be serialized.
  • Object. Serializes the related model and represents a configuration for the association.
    • pick. String, String[], Boolean, or Function. Includes the provided attribute/s.
    • omit. String, String[], Boolean, or Function. Omits the provided attribute/s.
    • assoc. Object or Function.
    • assocPick. Function.
    • assocOmit. Function.
    • deepAssoc. Boolean.

or a Function invoked with the following interface:

function (assocName, model)

and tests which associations it must include.

assocPick

A Function that tests which attributes for each association must be included.

function (assocName, value, key, model)

assocOmit

A Function that tests which attributes for each association must be excluded.

function (assocName, value, key, model)

deepAssoc

Boolean. Performs a deep serialization of the model and the relationships.

Building and Testing

First install locally all the required development dependencies.

npm install

Building

grunt

Backbone.Jsonify

grunt base

Supermodel.Jsonify

grunt supermodel

Testing

grunt test

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 and test your code using grunt.

Release History

Read the CHANGELOG.md file distributed with the project.

License

Read the LICENSE-MIT file distributed with the project.