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

v0.1.3

Published

Keep your Backbone models intact using strict property declaration without loosing flexibility with session and derived attributes.

Downloads

14

Readme

IntactModel

IntactModel is in most part inspired by Henrik Joreteg's HumanModel and would not have been made was it not for the pressing need for legacy support (I'm looking at you, IE8). IntactModel aims to help you keep your models intact by forcing strictly typed properties set to the model and at the same time allow for session attributes such as states or arbitrary data without persisting them to the server. Properties declaration aim to follow the JSON Schema for testing types. In no way is this a complete implementation of the JSON schema, only type is implemented at this time.

Properties

The aim is to support JSON schema but not at the price of excessive code bloat, hence, as of now IntactModel only tests against the type property. More properties could very well be added on later.

var Model = new Backbone.IntactModel({
  properties: {
    'foo': {type: 'string'}
  }
});

var model = new Model({
  'foo': []
});

model.attributes; // {}

Session attributes

IntactModel aims to be graceful in how it handles your attributes. If you try to set an attribute that is not declared on beforehand IntactModel simply sets it as a session attribute and allows you to get is just as if it were a "native" attribute. You never have to bother with separating "native" attributes from session attributes, IntactModel does that for you.

var Model = new Backbone.IntactModel({
  properties: {
    'foo': {type: 'string'}
  }
});

var model = new Model({
  'foo': 'foo',
  'bar': 'bar'
});

JSON.stringify(model);  // {"foo": "foo", "bar": "bar"}
model.attributes;       // {"foo": "foo"}
model.session;          // {"bar": "bar"}
model.save();           // Payload: {"foo": "foo"}

Derived attributes

IntactModel supports derived attributes that can compute and compile a value based on the model's other properties. This is best explained with an example:

var Model = new Backbone.IntactModel({
  properties: {
    'first': {type: 'string'},
    'second': {type: 'string'}
  },
  derived: {
    'greeting': function () {
      return this.get('first') + ' ' + this.get('last');
    }
  }
});

var model = new Model({
  'first': 'Hello',
  'second': 'world!'
});

model.get('greeting'); // "Hello world!"

Notes on model methods

extend

A common pattern is to extend upon common model classes. To be able to extend on the model's properties and derived attributes without overwriting them, a custom deep extend is the default extend method for IntactModel. In short, IntactModel.extend performs a regular Backbone.Model.extend and also manually extends on it's super's properties, derived and defaults.

If this behaviour is not what you want, simply call the default Backbone.Model's extend method, as so:

var MyModel = Backbone.Model.extend.call(SomeOtherModel, {
  properties: {
    'someAttribute': {type: 'string'}
  }
});

isComplete

A utility function for checking whether a model has all it's declared properties as defined attributes. Useful for determining if the model ought to be fetched to ensure complete data.

compile

The compile method is a way of getting all the model's data. Compile returns the the model's attributes, session and derived properties, all merged in to one object, in that order (meaning colliding attribute names we be overridden).

toJSON

The toJSON method is unaltered, meaning it only returns a clone of the model attributes.

validate

IntactModel has the function it uses for testing properties assigned as it's default validate method. Override this if you have other needs regarding validation.

clone

The clone method is unaltered, meaning it only clones model.attributes.

clear

Clearing a model behaves in essence in the same way as a default Backbone Model. Calling clear on a model simply unsets all its "native" and session attributes. However, passing in the option graceful unsets only the session attributes, leaving the model.attributes untouched.

var Model = new Backbone.IntactModel({
  properties: {
    'foo': {type: 'string'}
  }
});

var model = new Model({
  'foo': 'foo',
  'bar': 'bar'
});

// Gracefully clear the model
model.clear({graceful: true});

model.attributes; // {"foo": "foo"}
model.session;    // {}

// Completely clear the model
model.clear();

model.attributes; // {}

License

MIT