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

js-backbone-model

v1.2.5

Published

Better handling for backbone models with correct type casting.

Downloads

5

Readme

backbone-model

General

This model extends the Backbone.Model with some more features

  • attribute type safety
  • sub collection attributes
  • sub model attributes
  • attribute property observer
  • parametrized urls for loading and saving from server

Install

bower install backbone-model --save
bower install js-backbone-model --save

Usage

Attribute definition

For every model you must define the attribute types object, which maps the model attributes to a specific javascript type. Following types are available:

  • Backbone.ModelEx.ATTRIBUTE_TYPE_NUMBER ... defines this attribute as a number. This value will be converted to a native javascript number. If this converted value isNaN === true, a warning will be thrown.
  • Backbone.ModelEx.ATTRIBUTE_TYPE_STRING ... defines this attribute as a string. This value will be converted to a native javascript string.
  • Backbone.ModelEx.ATTRIBUTE_TYPE_BOOLEAN ... defines this attribute as a boolean. This value will be converted to a native javascript boolean. The string values "True", "TRUE", "true" will be converted to TRUE. The string values "False", "FALSE", "false" will be converted to FALSE. All other will be converted to an Number and then to Boolean if isNaN === false.
  • Backbone.ModelEx.ATTRIBUTE_TYPE_DATE ... defines this attribute as a date string (e.g. 2017-05-26), which can be parse by the Date Object. If the value can not be parsed, a warning will be thrown. The attribute will be converted to a Date Object.
  • Backbone.ModelEx.ATTRIBUTE_TYPE_DATETIME ... defines this attribute as a date time string (e.g. 2017-05-26T09:49:56.998Z), which can be parse by the Date Object. If the value can not be parsed, a warning will be thrown. The attribute will be converted to a Date Object.
  • Backbone.ModelEx.ATTRIBUTE_TYPE_TIME ... defines this attribute as a time string (e.g. 10:11:12), which can be parse by the Date Object. If the value can not be parsed, a warning will be thrown. The attribute will be converted to a Date Object.
  • Backbone.ModelEx.ATTRIBUTE_TYPE_COLLECTION ... defines this attribute as a Backbone Collection. This property must be an array. If it is not an array, a warning will be thrown. The attribute will be converted to a Backbone collection.
  • Backbone.ModelEx.ATTRIBUTE_TYPE_ARRAY ... defines this attribute as a javascript native array. This property must be an array. If it is not an array, a warning will be thrown.
  • Backbone.ModelEx.ATTRIBUTE_TYPE_MODEL ... defines this attribute as a Backbone Model. This property can be anything. The model must able to parse the data. The attribute will be converted to a Backbone Model.
  • Backbone.ModelEx.ATTRIBUTE_TYPE_IGNORE ... this attribute will be ignored. No conversion will be done for this attribute. The attribute will be untouched.

The attribute types will be defined by the property "attributeTypes".

If a model will be converted to JSON, every sub collection or sub model will be also converted to JSON and appended to the result object.

Example

This example shows,

  • how attribute types will be defined
  • how a attribute can be a collection and
  • how a attribute can be a model
// definition of Child Model
function ChildModel(attributes, options) {
    Backbone.ModelEx.apply(this, arguments);
}

ChildModel.prototype = Object.create(Backbone.ModelEx, {
    attributeTypes: {
        value: {
            id: Backbone.ModelEx.ATTRIBUTE_TYPE_NUMBER,
            name: Backbone.ModelEx.ATTRIBUTE_TYPE_STRING,
            birthday: Backbone.ModelEx.ATTRIBUTE_TYPE_DATE
        },
        enumerable: true,
        configurable: true,
        writable: true
    }
});

// definition of Children Collection
function ChildrenCollection() {
    this.model = ChildModel;
    Backbone.CollectionEx.apply(this, arguments);
}

ChildrenCollection.prototype = Object.create(Backbone.CollectionEx, {});

// definition of Parent Model
function ParentModel(attributes, options) {
    this.children = new ChildrenCollection();
    
    Backbone.ModelEx.apply(this, arguments);
}

ParentModel.prototype = Object.create(Backbone.ModelEx, {
    attributeTypes: {
        value: {
            id: Backbone.ModelEx.ATTRIBUTE_TYPE_NUMBER,
            text: Backbone.ModelEx.ATTRIBUTE_TYPE_STRING,
            birthday: Backbone.ModelEx.ATTRIBUTE_TYPE_DATE,
            children: Backbone.ModelEx.ATTRIBUTE_TYPE_COLLECTION,
            partner: Backbone.ModelEx.ATTRIBUTE_TYPE_MODEL
        },
        enumerable: true,
        configurable: true,
        writable: true
    },
    
    children: {
        enumerable: true,
        configurable: true,
        get: function() {
            if (this._children === undefined) {
                this._children = new ChildrenCollection();
            }
            return this._children;
        }
    },
    
    partner: {
        enumerable: true,
        configurable: true,
        get: function() {
            if (this._partner === undefined) {
                this._partner = new ParentModel();
            }
            return this._partner;
        }
    }
});

Attribute observation

Every model has the property "observer", which will be used for observation the attribute properties. The observer will not be created automatically. You must get the observer and bind your events to the observer.

Parametrized Urls

The model support parametrized urls for loading and saving from server. If you define a model, you can specify an url with parameter to the model it self and the model attributes. This applies for the properties "url" and "urlRoot".

Example

// definition of Child Model
function ChildModel(attributes, options) {
    this.url = '/api/model/child/:token';
    Backbone.ModelEx.apply(this, arguments);
}

ChildModel.prototype = Object.create(Backbone.ModelEx, {
    attributeTypes: {
        value: {
            id: Backbone.ModelEx.ATTRIBUTE_TYPE_NUMBER,
            name: Backbone.ModelEx.ATTRIBUTE_TYPE_STRING,
            birthday: Backbone.ModelEx.ATTRIBUTE_TYPE_DATE
        },
        enumerable: true,
        configurable: true,
        writable: true
    },
    
    token: {
        value: 'uitnqv398rcnq8thn437qfg78qghm39qhgf4893wx',
        enumerable: true,
        configurable: true,
        writable: true
    }
});

Addition Options

  • parseOnCreate ... define the default options.parse value for model creation. This value will be used in the constructor, if attributes will be defined. Default true
  • waitDefault ... define the default options.wait value for the functions "destroy", "fetch", "save"