Created
November 20, 2013 23:44
-
-
Save trshafer/7573330 to your computer and use it in GitHub Desktop.
back to coffee for rendr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ModelUtils = require('../../shared/modelUtils') | |
module.exports = class AddClassMapping | |
constructor: (@utils)-> | |
@utils ||= new ModelUtils | |
add: (key, modelConstructor)=> | |
@utils._classMap[@utils.underscorize(key)] = modelConstructor; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BaseModel = require("./base/model") | |
BaseCollection = require("./base/collection") | |
module.exports = class ModelUtils | |
constructor: (@entryPath)-> | |
@_classMap = {} | |
getModel: (path, attrs, options) -> | |
attrs = attrs or {} | |
options = options or {} | |
Model = @getModelConstructor(path) | |
new Model(attrs, options) | |
getCollection: (path, models, options) -> | |
models = models or [] | |
options = options or {} | |
Collection = @getCollectionConstructor(path) | |
new Collection(models, options) | |
getModelConstructor: (path) -> | |
path = ModelUtils.underscorize(path) | |
@_classMap[path] or require(@entryPath + "app/models/" + path) | |
getCollectionConstructor: (path) -> | |
path = ModelUtils.underscorize(path) | |
@_classMap[path] or require(@entryPath + "app/collections/" + path) | |
isModel: (obj) -> | |
obj instanceof BaseModel | |
isCollection: (obj) -> | |
obj instanceof BaseCollection | |
getModelNameForCollectionName: (collectionName) -> | |
Collection = @getCollectionConstructor(collectionName) | |
@modelName Collection::model | |
@uppercaseRe: /([A-Z])/g | |
@underscorize: (name) -> | |
return `undefined` unless name? | |
name = name.replace(@uppercaseRe, (c) -> | |
"_" + c.toLowerCase() | |
) | |
name = name.slice(1) if name[0] is "_" | |
name | |
underscorize: (name)-> | |
ModelUtils.underscorize(name) | |
### | |
The 'name' property is added to the constructor when using a named function, | |
and it cannot be changed. I.e.: | |
function MyClass(){} | |
MyClass.name | |
-> "MyClass" | |
We first look for the 'id' property of the constructor, which is compatible | |
with standard Backbone-style class inheritance. | |
var MyClass = Backbone.Model.extend({}); | |
MyClass.name | |
-> "" | |
MyClass.id = "MyClass" | |
### | |
modelName: (modelOrCollectionClass) -> | |
ModelUtils.underscorize modelOrCollectionClass.id or modelOrCollectionClass.name | |
modelIdAttribute: (modelName) -> | |
constructor = undefined | |
constructor = @getModelConstructor(modelName) | |
constructor::idAttribute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment