Skip to content

Instantly share code, notes, and snippets.

@grantges
Last active June 3, 2016 17:25
Show Gist options
  • Save grantges/859c9e6a53264e7fc0e7 to your computer and use it in GitHub Desktop.
Save grantges/859c9e6a53264e7fc0e7 to your computer and use it in GitHub Desktop.
Model Extension in Appcelerator Alloy
var modelOne = Alloy.createModel('modelOne');
var modelTwo = Alloy.createModel('modelTwo',{
name: "Bert",
description: "is the man",
URL:"thisisawesome.com"
});
Ti.API.info(modelTwo.config);
modelTwo.save();
$.index.open();
exports.definition = {
config: {
columns: {
"name": "string",
"description": "string"
},
adapter: {
type: "sql",
collection_name: "modelOne"
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
// extended functions and properties go here
});
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
// extended functions and properties go here
// For Backbone v1.1.2, uncomment the following to override the
// fetch method to account for a breaking change in Backbone.
/*
fetch: function(options) {
options = options ? _.clone(options) : {};
options.reset = true;
return Backbone.Collection.prototype.fetch.call(this, options);
}
*/
});
return Collection;
}
};
var Alloy = require('alloy');
var baseModel = Alloy.createModel('test');
Ti.API.info( baseModel.config);
exports.definition = {
config: {
columns: _.extend(baseModel.config.columns, {
"URL": "string"
}),
adapter: {
type: "sql",
collection_name: "test2"
}
},
extendModel: function() {
Ti.API.info('EXTEND MODEL');
var model = _.clone(baseModel.prototype);
_.extend(model, {
// your props/methods here
});
return model
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment