backbone.nested-types
Advanced tools
Comparing version 0.2.0 to 0.5.0
@@ -16,7 +16,41 @@ // Backbone.nestedTypes 0.2 (https://github.com/Volicon/backbone.nestedTypes) | ||
'use strict'; | ||
var extend = Backbone.Model.extend; | ||
function attachNativeProperties( This, properties, Base ){ | ||
_.each( properties, function( propDesc, name ){ | ||
var prop = typeof propDesc === 'function' ? { | ||
get: propDesc, | ||
enumerable: false | ||
} : propDesc; | ||
if( name in Base.prototype ){ | ||
throw new TypeError( 'extend: property ' + name + ' conflicts with base class members!' ); | ||
} | ||
Object.defineProperty( This.prototype, name, prop ); | ||
}); | ||
} | ||
function extendWithProperties( Base ){ | ||
return function( protoProps, staticProps ){ | ||
var This = extend.call( this, protoProps, staticProps ); | ||
attachNativeProperties( This, protoProps.properties, Base ); | ||
return This; | ||
}; | ||
} | ||
exports.Class = function(){ | ||
function Class(){ | ||
this.initialize.apply( this, arguments ); | ||
}; | ||
_.extend( Class.prototype, Backbone.Events, { initialize: function (){} } ); | ||
Class.extend = extendWithProperties( Class ); | ||
return Class; | ||
}(); | ||
exports.Collection = function(){ | ||
var Collection, | ||
CollectionProto = Backbone.Collection.prototype, | ||
extend = Backbone.Collection.extend; | ||
CollectionProto = Backbone.Collection.prototype; | ||
@@ -58,31 +92,4 @@ function wrapCall( func ){ | ||
function attachNativeProperties( This, spec ){ | ||
var properties = {}; | ||
Collection.extend = extendWithProperties( Collection ); | ||
if( spec.properties !== false ){ | ||
_.each( spec.properties, function( propDesc, name ){ | ||
properties[ name ] = typeof propDesc === 'function' ? { | ||
get: propDesc, | ||
enumerable: false | ||
} : propDesc; | ||
}); | ||
_.each( properties, function( prop, name ){ | ||
if( name in CollectionProto ){ | ||
throw new TypeError( 'extend: property ' + name + ' conflicts with Backbone.Collection base class members!' ); | ||
} | ||
Object.defineProperty( This.prototype, name, prop ); | ||
}); | ||
} | ||
} | ||
Collection.extend = function( protoProps, staticProps ){ | ||
var This = extend.call( this, protoProps, staticProps ); | ||
attachNativeProperties( This, protoProps ); | ||
return This; | ||
}; | ||
return Collection; | ||
@@ -89,0 +96,0 @@ }(); |
{ | ||
"name": "backbone.nested-types", | ||
"main": "nestedtypes.js", | ||
"description": "Native properties, defaults' type annotations, nested models and collections", | ||
"description": "backbone.js extension adding type annotations to model attributes, easiest possible way of dealing with nested models and collections, and native properties for attributes. Providing you with a more or less complete, simple, and powerful object system for JavaScript.", | ||
"homepage": "https://github.com/Volicon/backbone.nestedTypes", | ||
@@ -18,3 +18,3 @@ "keywords": [ "backbone", "relation", "nested", "model", "types", "properties" ], | ||
"license": "MIT", | ||
"version": "0.2.0" | ||
} | ||
"version": "0.5.0" | ||
} |
backbone.nestedTypes | ||
==================== | ||
In case you're precisely know what you're looking for, it's backbone.js extension adding model's native properties, type annotations, nested models and collections. | ||
In case you're precisely know what you're looking for, it's backbone.js extension adding type annotations to model attributes, easiest possible way of dealing with nested models and collections, and native properties for attributes. Providing you with a more or less complete, simple, and powerful object system for JavaScript. | ||
@@ -179,3 +179,50 @@ In case if you don't, here is a brief outline of problems we're solving with this little thing. There are two major goals behind: | ||
------------------ | ||
- deepClone operation for deep copy of nested models, collections, and types. | ||
- Default attributes are being inherited from the base class. | ||
- deepClone operation for deep copy of nested models, collections, and types. When you start working with nested stuff seriously, you'll need it soon. | ||
```javascript | ||
model.nestedModel = other.nestedModel.deepClone(); // will create a copy of nested objects tree | ||
``` | ||
- Default attributes are being inherited from the base model. In vanilla backbone, base model defaults will be completely overriden by subclass, which is annoying. | ||
```javascript | ||
var Base = Model.extend({ | ||
defaults: { | ||
a : 1 | ||
} | ||
}); | ||
var Derived = Base.extend({ | ||
defaults: { | ||
b : 1 | ||
} | ||
}); | ||
var instance = new Derived(); | ||
assert( instance.b === 1 ); | ||
assert( instance.a === 1 ); | ||
``` | ||
- Class type, which can send and receive Backbone events and can be extended. Also, it can have native properties, as Model and Collection. The basic building block of Backbone, which was not exported from the library directly for some reason. | ||
```javascript | ||
var myClass = Class.extend({ | ||
a : 1, | ||
initialize : function( options ){ | ||
this.a = options.a | ||
}, | ||
properties : { | ||
b : function(){ return this.a + 1; } | ||
} | ||
}); | ||
``` | ||
Installation and dependencies | ||
----------------------------- | ||
You need a single file (nestedtypes.js) and backbone.js itself. It should work in browser with plain script tag, | ||
require.js or other AMD loader. Also, it's available as npm package for node.js (https://www.npmjs.org/package/backbone.nested-types). | ||
Module exports three variables - Class, Model, Collection. You need to use them instead of backbone's. | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
911112
26147
228