Skip to content

Instantly share code, notes, and snippets.

@rmoura-92
Created July 30, 2013 13:08
Show Gist options
  • Save rmoura-92/6112734 to your computer and use it in GitHub Desktop.
Save rmoura-92/6112734 to your computer and use it in GitHub Desktop.
Model nesting
define([
'underscore',
'backbone',
], function(_, Backbone){
var ViagemModel = Backbone.Model.extend({
idAttribute: 'id',
initialize: function() {
that.getDetalhes();
},
parse: function(response, options) {
if(options.detail) {
return {detalhes:_.first(response)}
}
return response;
},
getDetalhes: function() {
//console.log('ola');
var that = this;
var route = 'getMinhasViagensDetail';
var item = this.toJSON();
var params = {
pFileNr: item.fileNr,
pId: item.id,
pType: item.type
};
this.fetch({
url: generateUrl(params, route),
detail: true,
success: function() {
//item = model.toJSON();
//that.set({detalhes: item});
that.trigger('complete');
return true;
},
error: function() {
return false;
}
});
}
});
return ViagemModel;
});
define([
'underscore',
'backbone',
'models/viagem',
], function(_, Backbone, ViagemModel){
var ViagensCollection = Backbone.Collection.extend({
model: ViagemModel,
url: function() {
return this.model.url;
},
parse: function(response, options) {
return response;
},
getViagens: function(login) {
var that = this;
var route = 'getMinhasViagens';
this.fetch({
url: generateUrl(login, route),
reset: true,
success: function(response) {
return response;
},
error: function() {
return false;
}
});
}
});
return ViagensCollection;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment