Socket
Socket
Sign inDemoInstall

backbone.nested-types

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone.nested-types - npm Package Compare versions

Comparing version 0.9.11 to 0.10.0

117

nestedtypes.js

@@ -1,2 +0,2 @@

// Backbone.nestedTypes 0.9.11 (https://github.com/Volicon/backbone.nestedTypes)
// Backbone.nestedTypes 0.10.0 (https://github.com/Volicon/backbone.nestedTypes)
// (c) 2014 Vlad Balin & Volicon, may be freely distributed under the MIT license

@@ -42,2 +42,3 @@

var This = extend.call( this, protoProps, staticProps );
delete This._subsetOf;

@@ -103,2 +104,7 @@ _.each( protoProps.properties, function( propDesc, name ){

function chainHooks( first, second ){
return function( value, name ){
return second.call( this, first.call( this, value, name ), name );
}
}
Nested.options = ( function(){

@@ -124,3 +130,3 @@ var Attribute = Nested.Class.extend({

spec.get = get ? function(){
return get.call( this, this.attributes[ name ] );
return get.call( this, this.attributes[ name ], name );
} : function(){

@@ -134,2 +140,8 @@ return this.attributes[ name ];

options : function( spec ){
if( spec.get && this.get ){
spec.get = chainHooks( this.get, spec.get );
}
if( spec.set && this.set ){
spec.set = chainHooks( this.set, spec.set );
}
_.extend( this, spec );

@@ -210,2 +222,5 @@ return this;

Nested.defaults = function( x ){
return Nested.Model.defaults( x );
};
Nested.value = function( value ){ return Nested.options({ value: value }); };

@@ -322,3 +337,3 @@

_bulkSet : function( attrs, options ){
if( attrs.constructor !== Object ){
if( Object.getPrototypeOf( attrs ) !== Object.prototype ){
Nested.error.argumentIsNotAnObject( this, attrs );

@@ -338,4 +353,7 @@ }

if( attrSpec.set && value !== this.attributes[ name ] ){
value = attrSpec.set.call( this, value, options );
if( value === undefined ) continue;
value = attrSpec.set.call( this, value, name );
if( value === undefined ){
delete attrs[ name ];
continue;
}
attrSpec.cast && ( value = attrSpec.cast( value, options, this ) );

@@ -373,3 +391,3 @@ }

if( attrSpec.set && value !== this.attributes[ name ] ){
value = attrSpec.set.call( this, value, options );
value = attrSpec.set.call( this, value, name );
if( value === undefined ) return this;

@@ -411,2 +429,14 @@ attrSpec.cast && ( value = attrSpec.cast( value, options, this ) );

constructor : function(attributes, options){
var attrs = attributes || {};
options || (options = {});
this.cid = _.uniqueId( 'c' );
this.attributes = {};
if( options.collection ) this.collection = options.collection;
if( options.parse ) attrs = this.parse( attrs, options ) || {};
attrs = _.defaults( {}, attrs, this.defaults( options ) );
this.set( attrs, options );
this.changed = {};
this.initialize.apply( this, arguments );
},
// override get to invoke native getter...

@@ -416,10 +446,10 @@ get : function( name ){ return this[ name ]; },

// Create deep copy for all nested objects...
deepClone: function(){
deepClone: function( options ){
var attrs = {};
_.each( this.attributes, function( value, key ){
attrs[ key ] = value && value.deepClone ? value.deepClone() : value;
attrs[ key ] = value && value.deepClone ? value.deepClone( options ) : value;
});
return new this.constructor( attrs );
return new this.constructor( attrs, options );
},

@@ -465,3 +495,3 @@

isValid : function( options ){
return ModelProto.isValid.call( this, options ) && _.every( this.attribute, function( attr ){
return ModelProto.isValid.call( this, options ) && _.every( this.attributes, function( attr ){
if( attr && attr.isValid ){

@@ -547,3 +577,7 @@ return attr.isValid( options );

return function(){
return function( options ){
if( options && ( options.collection || options.parse ) ){
options = _.omit( options, 'collection', 'parse' );
}
var defaults = literals();

@@ -554,3 +588,3 @@

for( var name in init ){
defaults[ name ] = init[ name ].create();
defaults[ name ] = init[ name ].create( null, options );
}

@@ -601,2 +635,3 @@

Model.defaults = function( attrs ){ return this.extend({ defaults : attrs }); };
return Model;

@@ -626,3 +661,3 @@ })();

Collection = Backbone.Collection.extend({
triggerWhenChanged: 'change add remove reset sort',
triggerWhenChanged: 'change add remove reset', // sort
__class : 'Collection',

@@ -650,3 +685,8 @@

set: wrapCall( CollectionProto.set ),
set: wrapCall( function( models, options ){
if( models && models instanceof Nested.Collection ){
models = models.models;
}
return CollectionProto.set.call( this, models, options );
}),
remove: wrapCall( CollectionProto.remove ),

@@ -663,2 +703,5 @@ add: wrapCall( CollectionProto.add ),

Collection.extend = createExtendFor( Collection );
Collection.defaults = function( attrs ){
return this.prototype.model.extend({ defaults : attrs }).Collection;
};

@@ -722,3 +765,3 @@ return Collection;

create : function( value, options ){
return arguments.length ? new this.type( value, options ) : new this.type();
return new this.type( value, options );
},

@@ -779,6 +822,3 @@

property : function( name ){
return {
get : function(){
var objOrId = this.attributes[ name ];
get : function( objOrId, name ){

@@ -800,8 +840,9 @@ if( typeof objOrId !== 'object' ){

set : function( modelOrId ){
this.set( name, modelOrId );
set : function( modelOrId, name ){
if( typeof modelOrId !== 'object' ){
var current = this.attributes[ name ];
if( current && typeof current === 'object' && current.id === modelOrId ) return;
}
return modelOrId;
}
}
return modelOrId;
}

@@ -816,3 +857,3 @@ });

var refsCollectionSpec = {
triggerWhenChanged : "add remove reset sort",
triggerWhenChanged : "add remove reset",
__class : 'Collection.SubsetOf',

@@ -855,7 +896,19 @@

addAll : function(){
this.reset( this.resolvedWith.models );
},
removeAll : function(){
this.reset();
},
justOne : function( arg ){
var model = arg instanceof Backbone.Model ? arg : this.resolvedWith.get( arg );
this.set( [ model ] );
},
set : function( models, upperOptions ){
var options = { merge : false };
if( models ){
if( models instanceof Array && models.length && typeof models[ 0 ] !== 'object' ){
options.parse = true;
options.merge = options.parse = true;
}
}

@@ -881,9 +934,10 @@

return function( masterCollection ){
var SubsetOf = this._subsetOf || ( this._subsetOf = this.extend( refsCollectionSpec ) );
var getMaster = parseReference( masterCollection );
return Nested.options({
type : this.extend( refsCollectionSpec ),
type : SubsetOf,
get : function( refs ){
refs.resolvedWith || refs.resolve( getMaster.call( this ) );
!refs || refs.resolvedWith || refs.resolve( getMaster.call( this ) );
return refs;

@@ -920,4 +974,7 @@ }

initialize : function(){
this.resolved = {};
this.installHooks();
},
installHooks : function(){
var self = this;
this.resolved = {};

@@ -955,3 +1012,3 @@ _.each( this.attributes, function( element, name ){

this.set( attrs );
this.installHooks();
return this;

@@ -958,0 +1015,0 @@ }

@@ -21,3 +21,3 @@ {

"license": "MIT",
"version": "0.9.11"
"version": "0.10.0"
}

@@ -1,14 +0,21 @@

IMPORTANT! Version 0.9.11 compatibility note
==================
There might be compatibility issues in you application when you upgrade to this release, so read this section carefully.
0.10.0 Release Notes
====================
- attribute get and set hooks
- they can be chained now. Thus, they work on Model.from and Collection.subsetOf
- they takes attribute name as second argument
- fixed bug in set hook (returning undefined didn't prevent attribute modification in some cases)
- Model
- Model.defaults() - added syntax for inline nested model definitions.
- .isValid - fixed exception
- .set now report error when not a plain object is passed as argument.
- .deepClone now pass options through the nested calls
- Model constructor now pass options to the nested constructors in defaults (except 'parse' and 'collection')
- Model.from: fixed bug (assignment of the same id to resolved reference caused unnecessary 'change' event)
- Collection:
- Collection.defaults() - added syntax for inline nested collection definitions.
- 'sort' event now doesn't count as nested attribute update, and won't bubble (it caused multiple problems)
- Collection.subsetOf improvements:
- Collections of different types now can be assigned to each other (model arrays will be passed to .set).
- Added set manipulation methods: addAll, removeAll, justOne.
Nested.Attribute is deprecated. Use:
- Nested.options({ ... }) instead of Nested.Attribute({ ... })
- Type.value( value ) instead of Nested.Attribute( Type, value )
- New semantic for attribute's get and set hooks. Previously, attribute options 'set' and 'get' used to override native properties. Please, refer to "get hook" and "set hook" topics.
- Model.from and Collection.subsefOf now started with lowercase letter, and will return null and [] when not resolved instead of dummy objects.
Except of these issues, upgrade should go fine. If you will encounter any problems during upgrade which are not covered here, don't hesitate to report a bug.
backbone.nestedTypes

@@ -480,3 +487,26 @@ ====================

```
### Inline nested Models and Collections definitions
Simple models and collections can be defined with special shortened syntax.
It's useful in case of deeply nested JS objects, when you previously preferred plain objects and arrays in place of models and collections. Now you could easily convert them to nested types, enjoying nested changes detection and 'deep update' features.
```javascript
var M = Nested.Model.extend({
defaults :{
nestedModel : Nested.defaults({ // define model extending base Nested.Model
a : 1,
b : MyModel.defaults({ //define model extending specified model
items : Collection.defaults({ // define collection of nested models
a : 1,
b : 2
})
})
})
}
})
```
### Attribute options

@@ -542,3 +572,3 @@ - type and value

get : function( value ){ return value; }
get : function( value, attrName ){ return value; }

@@ -549,3 +579,3 @@ Called on Model.get in the context of the model, allowing you to modify returned value.

set : function( value, options ){ return value; }
set : function( value, attrName ){ return value; }

@@ -552,0 +582,0 @@ Called on Model.set in the context of the model, allowing you to modify value before set ot cancel setting of the attribute, returning 'undefined'.

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