Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
backbone-relationship
Advanced tools
Backbone plugin that coerces attributes to models and collections
Backbone plugin that coerces attributes to models and collections
This library was built to solve handling relationships in a non-complex manner. We were frustrated at the excess amount of code in existing Backbone relationship libraries. This library focuses on one thing well, "converting objects/arrays to models/collections".
Install the module with: npm install backbone-relationship
// Extend Backbone and add our bindings
var BackboneRelationship = require('backbone-relationship');
var Backbone = BackboneRelationship.mixin(require('backbone'), require('underscore'));
// Define a model with a model relationship
var LocationModel = Backbone.Model.extend({
getFullName: function () {
return this.get('city') + ', ' + this.get('state');
}
});
var UserModel = Backbone.Model.extend({
relationships: {
location: LocationModel
}
});
// Create a user model
var user = new UserModel({location: {city: 'San Francisco', state: 'CA'}});
var location = user.get('location'); // LocationModel({city: 'San Francisco', state: 'CA'})
location.get('city'); // San Francisco
location.getFullName(); // San Francisco, CA
// Define a model with a collection relationship
var LocationCollection = Backbone.Collection.extend({model: LocationModel});
var UserModel = Backbone.Model.extend({
relationships: {
past_locations: LocationCollection
}
});
// Create a user model
var user = new UserModel({
past_locations: [{
city: 'San Francisco', state: 'CA'
}, {
city: 'Los Angeles', state: 'CA'
}]
});
var locations = user.get('past_locations'); // LocationCollection([{city: 'San Francisco', state: 'CA'}, ...])
locations.at(1); // LocationModel({city: 'Los Angeles', state: 'CA'})
locations.at(1).getFullName(); // Los Angeles, CA
More examples can be found in the Examples section.
Install the module with: bower install backbone-relationship
<script src="bower_components/backbone-relationship/dist/backbone-relationship.min.js"></script>
<script>
window.BackboneRelationship; // Use same as in `npm`
</script>
Download the minified JS at:
<script src="backbone-relationship.min.js"></script>
<script>
window.BackboneRelationship; // Use same as in `npm`
</script>
backbone-relationship
exposes the object BackboneRelationship
as its module.exports
.
BackboneRelationship.mixin(Backbone, _)
Extend Backbone and return Backbone with updated Model
and Collection
classes.
This will not mutate Backbone's Model
and Collection
classes.
Object
- Backbone library to mix into
Function
- Constructor for Backbone models to add new behavior toFunction
- Constructor for Backbone collections to add new behavior toObject
- Underscore library used by BackboneReturns:
Object
- Extension of Backbone
library passed in with extended Backbone.Model
and Backbone.Collection
classesModel.prototype.relationships
Any future models created from our extended Backbone will be allowed to coerce attributes from strings/objects into Models
/Collections
/any constructor.
Object
- Container to which attributes to be coerced
Mixed
- Each key/value pair will be treated as a key to coerce from a model's attributes
via its value (typically a constructor)
relationships: {location: LocationModel}
.set('location', {name: 'NYC'})
, we will generate a LocationModel({name: 'NYC'})
and save it at model.get('location')
.set
is run on initialize and any time an attribute is updatedrelationships: {created_at: Date}
.set('created_at', '2014-01-01')
, we will generate a new Date('2014-01-01')
and save it at model.get('created_at')
For more examples, please see the Examples section.
Model/Collection.prototype.inheritedOptions
Any future models/collections created from our extended Backbone will be allowed to pass options to their children.
Array
- Names of options to pass on from parent to child relationship
String
- Name of option to pass on
inheritedOptions: ['config'], relationships: {location: LocationModel}
new UserModel({location: {name: 'NYC}}, {config: 'hello', query: true})
.set('location', {name: 'NYC'})
, we will generate a LocationModel({name: 'NYC'}, {conifg: 'hello'})
. This means we will create a new set of options based off of the parent's options to pass through (i.e. {config: 'hello'}
)inheritedOptions: ['config'], model: UserModel
new UserCollection([{location: {name: 'NYC}}], {config: 'hello', query: true})
UserModel(attrs, {conifg: 'hello'})
(e.g. UserModel({location: {name: 'NYC'}}, {conifg: 'hello'})
)For more examples, please see the Examples section.
In order to make type coercion easy for some properties (e.g. Date
), we support invoking any function
as if it were a constructor (e.g. Date
, RegExp
). This example provides a few scenarios:
// Extend Backbone and add our bindings
var BackboneRelationship = require('backbone-relationship');
var Backbone = BackboneRelationship.mixin(require('backbone'), require('underscore'));
// Define a model with a Date
var ItemModel = Backbone.Model.extend({
relationships: {
created_at: Date
}
});
var item = new ItemModel({created_at: '2014-01-01'});
item.get('created_at'); // Date('2014-01-01'); Wed Jan 01 2014 00:00:00 GMT
// Define a model with a custom function
var ItemModel = Backbone.Model.extend({
relationships: {
tags: function (tags) {
// Split up a CSV by commas
return tags.split(/,/g);
}
}
});
var item = new ItemModel({tags: 'green,medium'});
item.get('tags'); // ['green', 'medium']
When using Backbone on the server, it is critical to pass around specific information between inherited models (e.g. external URL for server; underdog.io
). In this example, we will pass through an inherited option from a collection to a model to a submodel to demonstrate the depth of inheritance.
// Extend Backbone and add our bindings
var BackboneRelationship = require('backbone-relationship');
var Backbone = BackboneRelationship.mixin(require('backbone'), require('underscore'));
// Define our models/collections
var LocationModel = Backbone.Model.extend({
inheritedOptions: ['config'],
initialize: function (attrs, options) {
// Save the config for later
this._config = options.config;
// Call the default constructor
return Backbone.Model.prototype.initialize.call(this, attrs, options);
}
});
var UserModel = Backbone.Model.extend({
inheritedOptions: ['config'],
initialize: function (attrs, options) {
this._config = options.config;
return Backbone.Model.prototype.initialize.call(this, attrs, options);
},
relationships: {
location: LocationModel
}
});
var UserCollection = Backbone.Collection.extend({
initialize: function (models, options) {
this._config = options.config;
return Backbone.Collection.prototype.initialize.call(this, models, options);
},
inheritedOptions: ['config'],
model: UserModel
});
// Create a collection with a config
var users = new UserCollection([{
name: 'Bark Ruffalo',
location: {
name: 'New York City'
}
}], {config: {baseUrl: 'https://underdog.io/'}});
// Verify we have `config` saved on each collection/model
users._config; // {baseUrl: 'https://underdog.io/'}
var user = users.at(0); // new UserModel({name: 'Bark Ruffalo', location: ...}, {config: ...})
user._config; // {baseUrl: 'https://underdog.io/'}
var location = user.get('location'); // new LocationModel({name: 'New York City'}, {config: ...})
location._config; // {baseUrl: 'https://underdog.io/'}
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
.
Copyright (c) 2015 Underdog.io
Licensed under the MIT license.
FAQs
Backbone plugin that coerces attributes to models and collections
The npm package backbone-relationship receives a total of 1 weekly downloads. As such, backbone-relationship popularity was classified as not popular.
We found that backbone-relationship 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.