Skip to content

Instantly share code, notes, and snippets.

@BenConstable
Created June 25, 2012 13:15
Show Gist options
  • Save BenConstable/2988487 to your computer and use it in GitHub Desktop.
Save BenConstable/2988487 to your computer and use it in GitHub Desktop.
Neat wrapper for Backbone.js application bootstrapping. Very basic but works nicely!
/*
* 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));
/*
* 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