Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

backbone-model-factory

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone-model-factory

Provides a factory for generating model constructors that will never produce multiple instances of a model with the same unique identifier. Very useful for sharing model instances across views/modules.

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
719
decreased by-13.48%
Maintainers
1
Weekly downloads
 
Created
Source

Backbone.ModelFactory

Provides a factory for generating model constructors which will never produce multiple instances of a model with the same unique identifier. It allows a developer to manage model sharing between views more easily by maintaining an internal cache of model instances based on the value of their idAttribute property.

In short, when you create a new instance of a model created with Backbone.ModelFactory and give it an id it will never create duplicate instances of the same model with a given id.

var user1 = new User({id: 1});
var user2 = new User({id: 1});
var user3 = new User({id: 2});

console.log(user1 === user2); // true
console.log(user3 === user1); // false

Inclusion

Backbone.ModelFactory supports three methods of inclusion.

  1. Node:

     var Backbone = require('backbone-model-factory');
    
  2. AMD/RequireJS:

     require(['backbone-model-factory'], function (Backbone) {
         // Do stuff...
     });
    
  3. Browser Globals:

     <script src="path/to/backbone.js"></script>
     <script src="path/to/backbone-model-factory.js"></script>
    

Usage

Rather than extending Backbone.Model, constructors are created by a call to Backbone.ModelFactory. Instead of this:

var User = Backbone.Model.extend({
  defaults: {
    firstName: 'John',
    lastName: 'Doe'
  }
});

...do this:

var User = Backbone.ModelFactory({
    defaults: {
        firstName: 'John',
        lastName: 'Doe'
    }
});

Backbone.ModelFactory also supports inheritance, so model constructors can extend each other by providing a model constructor as the first argument:

var Admin = Backbone.ModelFactory(User, {
    defaults: {
        isAdmin: true
    }
});

Consequences of Extending Models

Models created with Backbone.ModelFactory will not share their unique-enforcement capabilities with models which they extend or which extend them. For example, using the User and Admin models above giving each the same ID would not result in the same object:

var user = new User({id: 1});
var admin = new Admin({id: 1});

console.log(user === admin); // false

The ability to share instances between models in an inheritance chain is a feature which is actively being considered, but it leaves a lot of open questions.

Tests

Inclusion

There are 3 files in /test/inclusion and they account for the 3 supported methods of including this module. To execute these tests, simply open the HTML files in a browser or install the npm module backbone and run node node-module.js.

Unit

Mocha unit tests exist in test/test.js. To run, simply do mocha from the project root.

Inspriation

This is inspired by SoundCloud's approach detailed in Building the Next SoundCloud under "Sharing Models between Views."

Keywords

FAQs

Package last updated on 01 Jan 2013

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc