Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Last active March 15, 2016 05:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save aaronksaunders/069433c5823444f5973e to your computer and use it in GitHub Desktop.
Save aaronksaunders/069433c5823444f5973e to your computer and use it in GitHub Desktop.
// extending Supermodel
var Model = Supermodel.Model.extend({
config : {
adapter : {}
}
});
// 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'
}
}
});
// 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'
}
}
});
// 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'
});
// Create Exam object and save it
var midtermExam = Exam.create({
name : "Midterm2013"
});
midtermExam.save();
// 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();
});
// 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;
}
});
$.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);
}
});
}
@troscoe
Copy link

troscoe commented Jun 24, 2013

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?

@CNIGIT
Copy link

CNIGIT commented Mar 10, 2016

what is this debugger?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment