Skip to content

Commit 747d953

Browse files
author
Vlad Balin
committed
fixes from Volicon develop
- replace:attr has wrong arguments - id attribute has not created. - subsetOf cache was not cleared on inheritance - native properties was not created in some cases -
1 parent fe4c37b commit 747d953

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

nestedtypes.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@
392392
// Override Backbone's objects .extend...
393393
[ 'Model', 'Collection', 'View', 'Router', 'History' ].forEach( function( name ){
394394
var BackboneType = Backbone[ name ];
395-
Object.extend.attach( BackboneType );
395+
if( BackboneType ) Object.extend.attach( BackboneType );
396396
});
397397

398398
Nested.Class = Object.extend.Class;
@@ -627,7 +627,7 @@
627627
if( this.__events ) model.listenTo( value, this.__events );
628628
}
629629

630-
model.trigger( 'replace:' + name, model, prev, value );
630+
trigger3( model, 'replace:' + name, model, value, prev );
631631
}
632632

633633
return value;
@@ -824,8 +824,10 @@
824824
properties : {
825825
id : {
826826
get : function(){
827-
var name = this.idAttribute; // TODO: add get event handling for id attr
828-
return name === 'id' ? this.attributes.id : this[ this.idAttribute ];
827+
var name = this.idAttribute;
828+
829+
// TODO: get hook doesn't work for idAttribute === 'id'
830+
return name === 'id' ? this.attributes.id : this[ name ];
829831
},
830832

831833
set : function( value ){
@@ -1103,24 +1105,26 @@
11031105

11041106
// Compile optimized constructor function for efficient deep copy of JSON literals in defaults.
11051107
_.each( attrSpecs, function( attrSpec, name ){
1106-
if( attrSpec.value !== undefined ){
1108+
if( attrSpec.value === undefined && attrSpec.type ){
1109+
// if type with no value is given, create an empty object
1110+
init[ name ] = attrSpec;
1111+
statements.push( 'this.' + name + '=i.' + name + '.create( o );' );
1112+
}
1113+
else{
11071114
// If value is given, type casting logic will do the job later, converting value to the proper type.
11081115
if( isValidJSON( attrSpec.value ) ){
11091116
// JSON literals must be deep copied.
11101117
statements.push( 'this.' + name + '=' + JSON.stringify( attrSpec.value ) + ';' );
11111118
}
1119+
else if( attrSpec.value === undefined ){
1120+
// handle undefined value separately. Usual case for model ids.
1121+
statements.push( 'this.' + name + '=undefined;' );
1122+
}
11121123
else{
11131124
// otherwise, copy value by reference.
11141125
refs[ name ] = attrSpec.value;
11151126
statements.push( 'this.' + name + '=r.' + name + ';' );
11161127
}
1117-
}
1118-
else{
1119-
// if type with no value is given, create an empty object
1120-
if( attrSpec.type ){
1121-
init[ name ] = attrSpec;
1122-
statements.push( 'this.' + name + '=i.' + name + '.create( o );' );
1123-
}
11241128

11251129
}
11261130
});
@@ -1231,8 +1235,16 @@
12311235

12321236
getModelIds : function(){ return _.pluck( this.models, 'id' ); }
12331237
},{
1238+
// Cache for subsetOf collection subclass.
1239+
__subsetOf : null,
12341240
defaults : function( attrs ){
12351241
return this.prototype.model.extend({ defaults : attrs }).Collection;
1242+
},
1243+
extend : function(){
1244+
// Need to subsetOf cache when extending the collection
1245+
var This = Backbone.Collection.extend.apply( this, arguments );
1246+
This.__subsetOf = null;
1247+
return This;
12361248
}
12371249
});
12381250

@@ -1273,7 +1285,7 @@
12731285
}
12741286
} )( this, this.name, this.get );
12751287
}
1276-
else Nested.options.Type.prototype.createPropertySpec.call( this );
1288+
else return Nested.options.Type.prototype.createPropertySpec.call( this );
12771289
},
12781290

12791291
cast : function( value, options, model, name ){
@@ -1471,7 +1483,7 @@
14711483
};
14721484

14731485
return function( masterCollection ){
1474-
var SubsetOf = this._subsetOf || ( this._subsetOf = this.extend( refsCollectionSpec ) );
1486+
var SubsetOf = this.__subsetOf || ( this.__subsetOf = this.extend( refsCollectionSpec ) );
14751487
var getMaster = parseReference( masterCollection );
14761488

14771489
return Nested.options({
@@ -1517,6 +1529,7 @@
15171529
var self = this;
15181530

15191531
_.each( this.attributes, function( element, name ){
1532+
if( !element ) return;
15201533
var fetch = element.fetch;
15211534
if( fetch ){
15221535
element.fetch = function(){

0 commit comments

Comments
 (0)