Model
extend (protoProps, staticProps)
Extends the Model with the following features defined via protoProps
:
types
: an object hash defining the types and attributes name which should be used for conversion when setting the values of those attributestypesDeep
: an deep object hash defining which deep object attributes should be converted; the individual deep properties types should be defined usingType.of(Constructor)
properties
: an array of attribute names which are just created as native properties on the Model
Arguments | ||
---|---|---|
protoProps | Object | |
staticProps | Object |
Returns
HyperModel
var Hyper = require('backbone-hyper-model');
var Person = Hyper.Model.extend({
types: {
name: String,
employed_at: Date,
salary: Number
}
});
var person = new Person();
// Types are created as native properties on the Model and are converted to their specific types
person.name = 'John';
person.employed_at = '2013-06-23 12:14:00';
console.log(person.employed_at.getTime()); // Attribute was converted to Date
person.salary = '123456';
console.log(typeof person.salary); // Number
var Hyper = require('backbone-hyper-model');
var Person = Hyper.Model.extend({
properties: ['gender', 'age']
});
var person = new Person();
// Properties are just created as native properties on the Model
person.gender = 'male';
person.age = 35;
var Hyper = require('backbone-hyper-model');
var Type = Hyper.Type;
var Person = Hyper.Model.extend({
typesDeep: {
dates: {
employed_at: Type.of(Date),
other: {
fired_at: Type.of(Date)
}
}
}
});
var person = new Person({
dates: {
employed_at: '2013-06-23 12:14:00',
other: {
fired_at: '2014-03-16 15:25:00'
}
}
});
// Deep types are only converted to their specific types
console.log(person.get('dates').employed_at.getTime()); // Attribute was converted to Date
console.log(person.get('dates').other.fired_at.getTime()); // Attribute was converted to Date
HyperModel.extend = function(protoProps, staticProps) {
var BaseProto = this.prototype;
protoProps._types = extend(true, {}, BaseProto._types || {});
if (protoProps.types) {
protoProps._types = extend(true, protoProps._types, protoProps.types);
delete protoProps.types;
}
protoProps._typesDeep = extend(true, {}, BaseProto._typesDeep || {});
if (protoProps.typesDeep) {
protoProps._typesDeep = extend(true, protoProps._typesDeep, protoProps.typesDeep);
delete protoProps.typesDeep;
}
protoProps._properties = (BaseProto._properties ? BaseProto._properties.slice() : []);
if (protoProps.properties) {
protoProps._properties = protoProps._properties.concat(protoProps.properties);
delete protoProps.properties;
}
var Model = originalExtend.call(this, protoProps, staticProps);
createNativeProperties(Model, protoProps);
return Model;
};
module.exports = HyperModel;
Type
of (ctor)
Initializer helper method
Arguments | ||
---|---|---|
ctor | Function |
Returns
Type
var Hyper = require('backbone-hyper-model');
var Type = Hyper.Type;
Type.of(Date); // returns a new Type object
Type.of = function(ctor) {
return new Type(ctor);
};
module.exports = Type;
Errors
propertyConflict (context, key)
Property conflict error handler
Arguments | ||
---|---|---|
context | Model | Model object which triggered the error |
key | String | The attribute key which caused the conflict |
// Sorry, no example available.
exports.propertyConflict = function(context, key) {
console.error('[backbone-hyper-model error](extend) Property ' +
key + ' conflicts with base class members');
};
unknownAttribute (context, key, value)
Unknown attribute error handler
Arguments | ||
---|---|---|
context | Model | Model object which triggered the error |
key | String | The attribute key which does not have a type defined |
value | Mixed | The value of the attribute key |
// Sorry, no example available.
exports.unknownAttribute = function(context, key, value) {
console.error('[backbone-hyper-model error](set) Attribute "' +
key + '" has no type.', value, 'In model', context);
};