Created
June 25, 2012 13:15
-
-
Save BenConstable/2988487 to your computer and use it in GitHub Desktop.
Neat wrapper for Backbone.js application bootstrapping. Very basic but works nicely!
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
/* | |
* Wrap up the Backbone application in a simple interface | |
*/ | |
// Using Jquery... | |
(function ($) { | |
// Create application | |
var MyApp = (function () { | |
var pub = {}; | |
var MyCollection = Backbone.Collection.extend({ | |
// ... | |
}); | |
var AppView = Backbone.View.extend({ | |
// ... | |
}); | |
pub.bootstrap = function (initialModels) { | |
this.MyCollection = new MyCollection; | |
// ... | |
// Bootstrap models | |
_.each(initialModels, function (collection) { | |
this[collection.name].reset(collection.models); | |
}, this); | |
// Start | |
this.AppView = new AppView; | |
}; | |
return pub; | |
}()); | |
// Expose | |
window.MyApp = MyApp; | |
}(jQuery)); |
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
/* | |
* And then to bootstrap, just include the following on your page | |
*/ | |
// If you're using PHP as your templating language | |
<script> | |
// Setup on ready... | |
jQuery(function () { | |
MyApp.bootstrap([ | |
{ | |
name: 'MyCollection', | |
models: <?php echo $myCollectionInitialModels; ?> | |
} | |
]); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment