-
-
Save aaronksaunders/069433c5823444f5973e to your computer and use it in GitHub Desktop.
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
// extending Supermodel | |
var Model = Supermodel.Model.extend({ | |
config : { | |
adapter : {} | |
} | |
}); |
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
// our Exam model extended from the Supermodel model we created above | |
var Exam = Model.extend({ | |
config : { | |
adapter : { | |
collection_name : 'exams' | |
} | |
} | |
}); | |
// our Question model extended from the Supermodel model we created above | |
var Question = Model.extend({ | |
config : { | |
adapter : { | |
collection_name : 'questions' | |
} | |
} | |
}); |
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
// Create the collections for the Questions, notice the model function added | |
var Questions = Collection.extend({ | |
model : function(attrs, options) { | |
return Question.create(attrs, options); | |
}, | |
config : { | |
adapter : { | |
collection_name : 'questions' | |
} | |
} | |
}); | |
// Create the collections for the Exams, notice the model function added | |
var Exams = Collection.extend({ | |
model : function(attrs, options) { | |
return Exam.create(attrs, options); | |
}, | |
config : { | |
adapter : { | |
collection_name : 'exams' | |
} | |
} | |
}); |
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
// establish the relationship between the Exam and the Questions | |
Exam.has().many('questions', { | |
collection : Questions, | |
inverse : 'exam' | |
}); | |
// establish the relationship between the Questions and the Exam | |
Question.has().one('exam', { | |
model : Exam, | |
inverse : 'questions' | |
}); |
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
// Create Exam object and save it | |
var midtermExam = Exam.create({ | |
name : "Midterm2013" | |
}); | |
midtermExam.save(); |
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
// simple loop to create questions and associate them | |
// based on the id of the exam, then save the question object | |
_.each(['one', 'two', 'three', 'four'], function(_item, _index) { | |
question = Question.create({ | |
exam_id : midtermExam.id, | |
title : "Question number " + _item | |
}); | |
question.save(); | |
}); |
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
// Now create Exam Collection and get the questions from out exam | |
var exams = new Exams(); | |
exams.fetch({ | |
success : function(_collection, _response) { | |
var exam = _collection.models[0]; | |
// dump the Exam object | |
Ti.API.info(JSON.stringify(exam.toJSON(), null, 2)); | |
// dump the Exam's Questions objects | |
Ti.API.info(JSON.stringify(exam.questions().toJSON(), null, 2)); | |
}, | |
error : function() { debugger; | |
} | |
}); |
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
$.index.open(); | |
debugger; | |
Supermodel = require('supermodel'); | |
// set the sync adapter | |
Alloy.Backbone.sync = function(method, model, opts) { debugger; | |
require("alloy/sync/properties").sync(method, model, opts); | |
}; | |
var Model = Supermodel.Model.extend({ | |
config : { | |
adapter : {} | |
} | |
}); | |
var Collection = Alloy.Backbone.Collection.extend({ | |
/** | |
* clean up any models from the properties db | |
*/ | |
cleanup : function() { | |
var regex = new RegExp("^(" + this.config.adapter.collection_name + ")\\-(.+)$"); | |
var TAP = Ti.App.Properties; | |
_.each(TAP.listProperties(), function(prop) { | |
var match = prop.match(regex); | |
if (match) { | |
TAP.removeProperty(prop); | |
Ti.API.info('deleting old model ' + prop); | |
} | |
}); | |
} | |
}); | |
var Exam = Model.extend({ | |
config : { | |
adapter : { | |
collection_name : 'exams' | |
} | |
} | |
}); | |
var Question = Model.extend({ | |
config : { | |
adapter : { | |
collection_name : 'questions' | |
} | |
} | |
}); | |
var Questions = Collection.extend({ | |
model : function(attrs, options) { | |
return Question.create(attrs, options); | |
}, | |
config : { | |
adapter : { | |
collection_name : 'questions' | |
} | |
} | |
}); | |
var Exams = Collection.extend({ | |
model : function(attrs, options) { | |
return Exam.create(attrs, options); | |
}, | |
config : { | |
adapter : { | |
collection_name : 'exams' | |
} | |
} | |
}); | |
// CLEAN UP OLD DATA | |
new Exams().cleanup(); | |
new Questions().cleanup(); | |
Exam.has().many('questions', { | |
collection : Questions, | |
inverse : 'exam' | |
}); | |
Question.has().one('exam', { | |
model : Exam, | |
inverse : 'questions' | |
}); | |
// create the objects | |
var question, midtermExam = Exam.create({ | |
name : "Midterm2013" | |
}); | |
midtermExam.save(); | |
_.each(['one', 'two', 'three', 'four'], function(_item, _index) { | |
question = Question.create({ | |
exam_id : midtermExam.id, | |
title : "Question number " + _item | |
}); | |
question.save(); | |
}); | |
// fetch the objects | |
var exams = new Exams(); | |
exams.fetch({ | |
success : function(_model, _response) { debugger; | |
Ti.API.info(JSON.stringify(_model.models[0].toJSON(), null, 2)); | |
Ti.API.info(JSON.stringify(_model.models[0].questions().toJSON(), null, 2)); | |
}, | |
error : function() { debugger; | |
} | |
}); | |
/** | |
* clean up any models from the properties db | |
* | |
* @param {Object} _prefix | |
*/ | |
function cleanup(_prefix) { | |
var regex = new RegExp("^(" + _prefix + ")\\-(.+)$"); | |
var TAP = Ti.App.Properties; | |
_.each(TAP.listProperties(), function(prop) { | |
var match = prop.match(regex); | |
if (match) { | |
TAP.removeProperty(prop); | |
Ti.API.info('deleting old model ' + prop); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you run once and then remove cleanup and creation exams.fetch does not print out questions()
[INFO] {
"id": "72cbda59-19a3-90eb-b4e0-c7cef866b158",
"name": "Midterm2013"
}
[]
How can you fetch the exam and associated questions from storage?