NestedTypes

Next generation data framework

Dates

Plain Backbone

  • No nested models
  • No support for Date type
  • No subviews

Basics

Native properties

  • are created for default model attributes
  • explicitly declared for Model and Collection
  • serve as calculated properties
  • direct support for lazy evaluation pattern

Lazy initialization

var C = Nested.Collection.extend({
    properties : {
        index : function(){
            if( !this._index ){
                this._index = new Index( this );

                this.listenTo( this, 'change add remove reset', function(){
                    this._index.rebuild( this );
                });
            }

            return this._index;
        }
    }
});



var c = new C(); // no index at this point...

if( c.index.has( 'name' ) ){ // c.index is created 
    ...
}

Model.defaults

var UserInfo = NestedTypes.Model.extend({
   defaults : {
       name : 'test'
    }
});
var user = new DetailedUserInfo();
console.assert( user.name === 'test' );
user.name = 'admin';
user.roles.push( 'admin' );
<-- user.name native property is created
<-- JSON literals are deep copied
<-- name:'test' is inherited from the base model
var DetailedUserInfo = UserInfo.extend({
    defaults: {
        login : '',
        roles : [ 'user' ]
    }
});

Model.Collection

  • is created automatically for every Model
  • can be customized with Model.collection property
  • is inherited from the base model's Collection
var UserInfo = NestedTypes.Model.extend({
   defaults : {
       name : 'test'
    }
});

​var users = new UserInfo.Collection();

Model.properties

  • explicit native properties definition
  • overrides auto-generated native properties
  • also works for Collection

Class type

  • has backbone-style .extend and .initialize
  • can have native properties (Class.properties)
  • can trigger and listen to backbone events

Run-time errors

  • Attributes must be defined in defaults.
  • Model.set argument must be literal JS object.
  • Native properties cannot override base class methods.

Type Annotations

Example: handling the Date

var User = Nested.Model.extend({
    defaults : {
        created    : Date,
        name       : "",
        loginCount : 0
    }
});
  • On new User() - it creates the Date object.
  • On user.set and user.fetch() - it parse ISO UTC+0 dates from strings, integer timestamps, and MS timestamps.
  • On user.save() - it serialize model.created to ISO UTC+0 datetime string.

Type specs...

  • ...are as simple as just mentioning value's constructor in Model.defaults.
  • ...is the guarantee that attribute always holds value of designated type, or null.
  • ...free you from writing custom .parse() and .toJSON() in most situations.

Constructor Type Spec

var A = Nested.Model.extend({
    defaults : {
        a : Ctor,
        b : Ctor.value( null ),
        c : Ctor.value( "something" )
    }
});
new Ctor() -->
null -->
new Ctor( "something" ) -->
defaults
null -->
obj -->
new Ctor( 135 ) -->
var model = new A(),
    obj = new Ctor();

model.a = null;
model.b = obj;
model.c = 135;
Type cast on 'set' -->

Primitive Types

var A = Nested.Model.extend({
    defaults : {
        str : "",
        num : 0,
        bool: false,
        int : Integer.value( 5 )
    }
});
"34" -->
234 -->
true -->
7 -->
var model = new A();

model.str = 34;
model.num = "234";
model.bool = "y";
model.int = 5.6;
String
Number
Boolean
<-- Automatic type detection
Type cast on 'set' -->
var DetailedUserInfo = UserInfo.extend({
    urlBase : '/api/detailed_user/',

    defaults : {
        login : '',
        roles : [ 'user' ]
    },

    collection : {
        initialize : function(){
            this.fetch();
        }
    }
});

Nested Models

and Collections

Relations by id

Attribute options

NestedTypes

By Vlad Balin

NestedTypes

  • 1,100