Last active
August 29, 2015 14:03
-
-
Save egeste/e0351634c1fa3eb93163 to your computer and use it in GitHub Desktop.
Referencing FactoryJS definitions by name
This file contains 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
# Create a simple definition based on the Model definition. | |
# The provided definition will be extended onto the base definition when an | |
# instance is requested from the factory. | |
Oraculum.extend 'Model', 'Custom.Model', { | |
# Provide a simple method to illustrate that the instance gets composed. | |
quack: -> console.log 'quack', @id | |
} | |
# Create another simple definition based on the Collection definition. | |
Oraculum.extend 'Collection', 'Singleton.Collection', { | |
# Reference our custom model definition by name as a string. | |
# It will automagically get resolved to a factory definition constructor. | |
model: 'Custom.Model' | |
# Provide a simple method to illustrate that the instance gets composed. | |
quack: -> @invoke 'quack' | |
# Make this definition a singleton. | |
# After the first time this definition has been constructed, all other requests | |
# for this definition will return the same instance. | |
}, singleton: true | |
# Request an instance of our Singleton.Collection definition so we can add some | |
# models to it. | |
singleton = Oraculum.get 'Singleton.Collection' | |
singleton.add id: 1 | |
singleton.add id: 2 | |
singleton.add id: 3 | |
# Invoke quack to show that all of our additions have resolved to model | |
# instances. | |
singleton.quack() | |
# Create a new view (modified Backbone.View), passing our singleton definition | |
# name as the collection. | |
view = Oraculum.get 'View', | |
collection: 'Singleton.Collection' | |
# Show that the view's collection has resolved to the same instance as our | |
# previous singleton. | |
console.log 'Singleton resolved?', view.collection is singleton |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment