Skip to content

The plugin is for defining model's properties with type specifying

License

Notifications You must be signed in to change notification settings

testOrganizationSetup/Backbone.Schema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backbone.Schema Build Status

The plugin is for defining model's properties with type specifying.

Dependencies:

Reference API

Backbone.Model

Static members

  • Object formatters
    • Function string
    • Function number
    • Function boolean
    • Function date
    • Function text
    • Function currency
    • Function percent
  • Object converters
    • Function string
    • Function number
    • Function boolean
    • Function date
    • Function text
    • Function currency
    • Function percent

Instance members

  • Function property(attribute, type)
    • String attribute
    • String type
  • Function computed(attribute, options)
    • String attribute
    • Object options
      • Function getter
      • Function setter
  • Function toJSON([options])
    • Object options
      • Boolean schema

Getting Started

Create model

var model = new Backbone.Model();

Define properties

Type string

Converts value to the string. Represents as is.

model.property('stringProperty', 'string');

model.set('stringProperty', 999999.99); // model.attributes.stringProperty -> "999999.99"
model.get('stringProperty'); // "999999.99"

Type number

Converts value to the number. Represents as the string in a format of current culture.

model.property('numberProperty', 'number');

model.set('numberProperty', '999,999.99'); // model.attributes.numberProperty -> 999999.99
model.get('numberProperty'); // "999,999.99"

Type boolean

Converts value to the boolean. Represents as is.

model.property('booleanProperty', 'boolean');

model.set('booleanProperty', 'true'); // model.attributes.booleanProperty -> true
model.get('booleanProperty'); // true

Type date

Converts value to the Unix time. Represents as the string in a format of current culture.

model.property('dateProperty', 'date');

model.set('dateProperty', '12/12/2012'); // model.attributes.dateProperty -> 1355263200000
model.get('dateProperty'); // "12/12/2012"

Type text

Converts value to the string, escaping an unsafe characters. Represents an unescaped string.

model.property('textProperty', 'text');

model.set('textProperty', '<b>text</b>'); // model.attributes.textProperty -> "&lt;b&gt;text&lt;&#x2F;b&gt;"
model.get('textProperty'); // "<b>text</b>"

Type currency

Converts value to the number. Represents as the string in a format of current culture.

model.property('currencyProperty', 'currency');

model.set('currencyProperty', '$999,999.99'); // model.attributes.currencyProperty -> 999999.99
model.get('currencyProperty'); // "$999,999.99"

Type percent

Converts value to the hundredths of number. Represents as the string in a format of current culture.

model.property('percentProperty', 'percent');

model.set('percentProperty', '99.99 %'); // model.attributes.percentProperty -> 0.9999
model.get('percentProperty'); // "99.99 %"

Define custom data type

// Define formatter
Backbone.Model.formatters.hex = function (attribute, value) {
    return '0x' + value.toString(16).toUpperCase();
};

// Define converter
Backbone.Model.converters.hex = function (attribute, value) {
    return parseInt(value, 16);
};

Custom type hex

model.property('hexProperty', 'hex');

model.set('hexProperty', '0xFF'); // model.attributes.hexProperty -> 255
model.get('hexProperty'); // "0xFF"

Convert model to JSON

Without options toJSON works as original method.

model.toJSON();
{
    "stringProperty": "string",
    "numberProperty": 999999.99,
    "booleanProperty": true,
    "dateProperty": 1355263200000,
    "textProperty": "&lt;b&gt;text&lt;&#x2F;b&gt;",
    "currencyProperty": 999999.99,
    "percentProperty": 0.9999,
    "hexProperty": 255
}

With { schema: true } option method toJSON will return a formatted representation.

model.toJSON({ schema: true });
{
    "stringProperty": "string",
    "numberProperty": "999,999.99",
    "booleanProperty": true,
    "dateProperty": "12/12/2012",
    "textProperty": "<b>text</b>",
    "currencyProperty": "$999,999.99",
    "percentProperty": "99.99 %",
    "hexProperty": "0xFF"
}

Define computed property

// Create model
var user = new Backbone.Model({
    firstName: 'Dmytro',
    lastName: 'Nemoga'
});

// Define computed property
user.computed('fullName', {
    getter: function (attribute, value) {
        var firstName = this.get('firstName'),
            lastName = this.get('lastName');

        return firstName + ' ' + lastName;
    },

    setter: function (attribute, value) {
        var fullName = value.split(' ');

        return {
            firstName: fullName[0],
            lastName: fullName[1]
        };
    }
});

// Get computed property
user.get('fullName'); // "Dmytro Nemoga"

// Set computed property
user.set('fullName', 'Andriy Serputko'); // user.attributes -> { firstName: "Andriy", lastName: "Serputko" }

Keeping integrity

The plugin prevents setting undefined values, instead of this it assigns a default value or null.

Changelog

0.2.0

  • Static methods runs in correct context, now they may be used as independent helpers

0.1.9

  • Properties formatters and converters is static

0.1.8

  • Removed CommonJS support

0.1.7

  • Added CommonJS support

0.1.6

  • Integration with project Backbone.Accessors
  • Method defineProperty renamed to property
  • Methods addGetter/addSetter merged to method computed
  • Option advanced of toJSON method renamed to schema

0.1.2

  • Removed argument options of defineProperty method's

0.1.1

  • Method addProperty renamed to defineProperty

0.1.0

  • Initial release

About

The plugin is for defining model's properties with type specifying

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published