backbone-decorators
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "backbone-decorators", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "ES2016 Decorators for Backbone and Marionette", | ||
@@ -24,2 +24,3 @@ "main": "dist/backbone-decorators.js", | ||
"dependencies": { | ||
"backbone": "1.0.0 - 1.2.3", | ||
"underscore": "1.4.4 - 1.8.3" | ||
@@ -26,0 +27,0 @@ }, |
import _ from 'underscore'; | ||
import Backbone from 'backbone'; | ||
@@ -35,2 +36,62 @@ /* Ideally we'd want to just pass these exports through directly | ||
//Collections | ||
export function model(modelClass) { | ||
return function decorator(target) { | ||
if (modelClass.prototype instanceof Backbone.Model) { | ||
target.prototype.model = modelClass; | ||
} else { | ||
throw new Error('The model decorator takes either a single argument that should be an instance of Backbone.Model'); | ||
} | ||
}; | ||
} | ||
export function comparator(comparatorString) { | ||
return function decorator(target) { | ||
if (_.isString(comparatorString)) { | ||
target.prototype.comparator = comparatorString; | ||
} else { | ||
throw new Error('The comparator decorator takes either a single argument that should be a string'); | ||
} | ||
}; | ||
} | ||
//Models | ||
export function defaults(...args) { | ||
return function decorator(target) { | ||
let defaults = target.prototype.defaults || {}; | ||
let [key, value] = args; | ||
if (_.isObject(key)) { | ||
_.extend(defaults, key); | ||
} else if (_.isString(key) && !_.isUndefined(value)) { | ||
defaults[key] = value; | ||
} else { | ||
throw new Error('The defaults decorator takes either a single object as an argument or a key and value'); | ||
} | ||
target.prototype.defaults = defaults; | ||
}; | ||
} | ||
//Router | ||
export function route (routeName){ | ||
return function(target, name, descriptor){ | ||
if(!target.routes) { | ||
target.routes = {}; | ||
} | ||
if(_.isFunction(target.routes)) { | ||
throw new Error('The route decorator is not compatible with a route method'); | ||
return; | ||
} | ||
if(!routeName) { | ||
throw new Error('The route decorator requires an route string argument'); | ||
} | ||
target.routes[routeName] = name; | ||
return descriptor; | ||
} | ||
} | ||
// Marionette Decorators | ||
@@ -120,1 +181,36 @@ | ||
} | ||
//Backbone.LocalStorage Decorators | ||
export function localStorage(storageKey) { | ||
return function decorator(target) { | ||
if (Backbone.LocalStorage) { | ||
if (_.isString(storageKey)) { | ||
target.prototype.localStorage = new Backbone.LocalStorage(storageKey); | ||
} else { | ||
throw new Error('The localStorage decorator requires a single string argument which will serve as the localStorage key'); | ||
} | ||
} else { | ||
throw new Error('The localStorage decorator requires Backbone.LocalStorage to have been loaded before use'); | ||
} | ||
}; | ||
} | ||
//Marionette-service Decorators | ||
export function replyRadio(channel, requestString) { | ||
return function(target, name, descriptor) { | ||
if (!target.radioRequests) { | ||
target.radioRequests = {}; | ||
} | ||
if (_.isFunction(target.radioRequests)) { | ||
throw new Error('The replyRadio decorator is not compatible with a radioRequests method'); | ||
return; | ||
} | ||
if (!_.isString(channel) || !_.isString(requestString)) { | ||
throw new Error('The replyRadio decorator requires 2 arguments, a channel and a request string.'); | ||
} | ||
target.radioRequests[channel + ' ' + requestString] = name; | ||
return descriptor; | ||
}; | ||
} |
48821
2
22
516
+ Addedbackbone@1.0.0 - 1.2.3
+ Addedbackbone@1.2.3(transitive)