Skip to content

Commit

Permalink
Merge branch 'create-model-transform-support'
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkneen committed Apr 4, 2017
2 parents 5a0fa15 + 06de7d0 commit 7b6625a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reste",
"version": "1.4.0",
"version": "1.4.1",
"description": "A JavaScript REST / API helper for Titanium with Alloy Models/Collections support",
"titaniumManifest": {
"guid": "afafe8b0-b93b-771c-a9e5-4e71db81b9ff"
Expand Down
26 changes: 25 additions & 1 deletion reste.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var main = function() {

// open the url and check if we're overrding with
// a local http based url

if (args.url.indexOf("http") >= 0) {
http.open(args.method, args.url);
} else {
Expand Down Expand Up @@ -365,6 +366,29 @@ var main = function() {

reste.createModel = function(name, attributes) {
var model = new Backbone.Model(attributes);

// if we have a config based transfor for th emodel
// then attach this to the model, or create a default
if (reste.modelConfig && reste.modelConfig[name].transform) {
model.transform = function(model, transform) {
if (transform) {
this.__transform = transform(this);
} else {
this.__transform = reste.modelConfig[name].transform(this);
}
return this.__transform;
};
} else {
model.transform = function(model, transform) {
if (transform) {
this.__transform = transform(this);
} else {
this.__transform = this.toJSON();
}
return this.__transform;
};
}

model._type = name;
return model;
};
Expand Down Expand Up @@ -401,7 +425,7 @@ var main = function() {
} else if (args.transform) {
this.__transform = args.transform(this);
} else {
this.__transform = this.toJSON()
this.__transform = this.toJSON();
}
return this.__transform;
}
Expand Down

0 comments on commit 7b6625a

Please sign in to comment.