Skip to content

Instantly share code, notes, and snippets.

@supabok
Created July 18, 2012 13:51
Show Gist options
  • Select an option

  • Save supabok/3136338 to your computer and use it in GitHub Desktop.

Select an option

Save supabok/3136338 to your computer and use it in GitHub Desktop.
Test BackboneJS+Intravenous implementation
//initialization of DI(intravenous) and command classes
$(document).ready(function() {
//create DI
var container = intravenous.create();
//we want this object to exist as a singleton throughout the app lifecycle
container.register("currentQuestionItem", {dataObj:''}, "singleton");
container.register("currentQuesVal", {score:-1000}, "singleton");
container.register("currentSlideIndex", {slideIndex:0}, "singleton");
//add new DI to prototype using underscore's extend method
_.extend(Drafted.Commands.Base.prototype.injector, container);//all command classes inherit injector
_.extend(Drafted.Views.QuestionSliderView.prototype.injector, container);
//create BaseCommand for command storage
var commandMap = Drafted.Commands.Base;
//store commands for future ref in base.commands
commandMap.register('saveScoreCommand', new Drafted.Commands.SaveScoreCommand);
commandMap.register('updateAnswerCommand', new Drafted.Commands.UpdateAnswerCommand);
commandMap.register('currentScoreCommand', new Drafted.Commands.CurrentScoreCommand);
});
//Sample command class with injector available - this is more of a "serviceLocator"
Drafted.Commands.UpdateAnswerCommand = Drafted.Commands.Base.extend({
quesObj: '',
answerId: '',
quesId: '',
execute: function(_data){
var that = this;
//grab currentQuestionItem dependency from injector
var currItem = this.injector.get("currentQuestionItem");
if(_.isUndefined(_data)){
this.quesId = this.currItem['dataObj'].getQuestionId();
} else {
this.quesObj = _data.question.getViewData();
this.answerId = _data.answerid;
this.quesId = this.quesObj.questionId;
}
$.ajax{
url: 'somewhere',
dataType: "json",
type: 'POST',
data: {'id':this.quesId}
})
})
//initialization of DI(intravenous) and command classes
$(document).ready(function() {
//create DI
var container = intravenous.create();
//we want this object to exist as a singleton throughout the app lifecycle
container.register("currentQuestionItem", {dataObj:''}, "singleton");
container.register("currentQuesVal", {score:-1000}, "singleton");
container.register("currentSlideIndex", {slideIndex:0}, "singleton");
//add new DI to prototype using underscore's extend method
_.extend(Drafted.Commands.Base.prototype.injector, container);//all command classes inherit injector
_.extend(Drafted.Views.QuestionSliderView.prototype.injector, container);
//create BaseCommand for command storage
var commandMap = Drafted.Commands.Base;
//store commands for future ref in base.commands
commandMap.register('saveScoreCommand', new Drafted.Commands.SaveScoreCommand);
commandMap.register('updateAnswerCommand', new Drafted.Commands.UpdateAnswerCommand);
commandMap.register('currentScoreCommand', new Drafted.Commands.CurrentScoreCommand);
commandMap.$inject = ["currentSlideIndex","currentQuestionItem", "currentQuesVal"];
});
//Sample command class with injector available - this is more of a "serviceLocator"
Drafted.Commands.UpdateAnswerCommand = Drafted.Commands.Base.extend({
quesObj: '',
answerId: '',
quesId: '',
execute: function(_data){
var that = this;
//grab currentQuestionItem dependency from injector
var currItem = this.injector.get("currentQuestionItem");
if(_.isUndefined(_data)){
this.quesId = this.currItem['dataObj'].getQuestionId();
} else {
this.quesObj = _data.question.getViewData();
this.answerId = _data.answerid;
this.quesId = this.quesObj.questionId;
}
$.ajax{
url: 'somewhere',
dataType: "json",
type: 'POST',
data: {'id':this.quesId}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment