Skip to content

Instantly share code, notes, and snippets.

@basketofsoftkittens
Last active December 16, 2015 13:08
Show Gist options
  • Save basketofsoftkittens/5439156 to your computer and use it in GitHub Desktop.
Save basketofsoftkittens/5439156 to your computer and use it in GitHub Desktop.
Interface for storing data from the server and loading into the appropriate model when instantiated
App.Data = Backbone.Model.extend({
})
// on the server when the page loads
$(function(){
require('App',function(){
App.Data.set({
BroModel:{greeting:'BRAAAAHHHHH!!!'}
})
})
})
//create a base model
define('BaseModel',['App'],function(App){
return Backbone.Model.extend({
getModelData:function(){
return App.Data.get(this.modelName);
}
})
})
// #create a specific model to fetch the data from the local store. if not there, get the data from the server.
define('BroModel',['App','BaseModel'],function(App, BaseModel){
return BaseModel.extend({
name:'BroModel',
initialize:function(){
var attrs = this.getModelData();
if (attrs && !_.isEmpty(attrs)) {
this.set(attrs);
} else {
this.fetch();
}
},
})
})
define('SupView',['BroModel'],function(BroModel){
return Backbone.View.extend({
initialize:function(){
this.model = new BroModel();
// Sup BRAAHHH!!
alert( 'Sup' + this.model.get('greeting'));
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment