Last active
December 16, 2015 13:08
-
-
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
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
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