Skip to content

Instantly share code, notes, and snippets.

@linus-amg
Created April 8, 2015 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linus-amg/68cbc3552a9db0868b54 to your computer and use it in GitHub Desktop.
Save linus-amg/68cbc3552a9db0868b54 to your computer and use it in GitHub Desktop.
var App = new Marionette.Application({
regions: {
'detail': '#detail'
}
});
var doc1 = {
"_id": "123",
"type": "customer",
"datosGenerales": {
"nombre": "Cliente 1",
"rfc": "RFC1010101H8A"
},
"direcciones": [
{
"calle": "Reforma 2654",
"colonia": "Lomas Altas",
"pais": "Mexico"
},
{
"calle": "Tehuantepec 118",
"colonia": "Roma Sur",
"pais": "Mexico"
}
]
};
var doc2 = {
"_id": "456",
"type": "customer",
"datosGenerales": {
"nombre": "Cliente 2",
"rfc": "RFC1010103H8A"
},
"direcciones": [
{
"calle": "Test 1",
"colonia": "Lomas Altas",
"pais": "Mexico"
},
{
"calle": "Test 3",
"colonia": "Roma Sur",
"pais": "Mexico"
}
]
};
var myData = [{
"_id": "456",
"type": "customer",
"datosGenerales": {
"nombre": "Cliente 2",
"rfc": "RFC1010103H8A"
} }, {
"_id": "123",
"type": "customer",
"datosGenerales": {
"nombre": "Cliente 1",
"rfc": "RFC1010101H8A"
}
}
];
var data = [];
function getDoc(_id){
if (_id == '123'){
return doc1;
} else
if (_id == '456'){
return doc2;
} else return null;
}
var EditView = Marionette.ItemView.extend({
template: Handlebars.compile($('#edit-item-template').text()),
initialize: function() {
this.listReference = this.options.oldModel;
this.render();
},
events: {
'submit form': '_submit'
},
_submit: function(e) {
e.preventDefault();
var $target = $(e.currentTarget);
var dataX = $target.serializeObject();
this.model.set(dataX);
this.listReference.set(dataX);
this.model.save();
return false;
}
});
App.editView = '';
var Model = Backbone.DocumentModel.extend({
idAttribute: 'id',
localStorage: new Backbone.LocalStorage("myDoc"),
initialize: function(options) {
this.options = options;
if (!this.get('id')) {
this.set('id', this.get('type') + '_' + this.get('_id'));
}
},
editView: function(oldModel) {
App.getRegion('detail').show(new EditView({ model: this, oldModel: oldModel }));
}
});
var Collection = Backbone.DocumentCollection.extend();
var collection = new Collection(myData);
var ListItem = Marionette.ItemView.extend({
template: Handlebars.compile($('#list-item-template').text()),
initialize: function() {
this.listenTo(this.model, 'all', this.render);
},
events: {
'click .name': '_accion',
'click .delete': 'destroy',
'click .edit': '_editMode'
},
_accion: function() {
//var currentModel = new Model(this.model.toJSON());
//currentModel.save();
alert(this.model.get('datosGenerales.rfc'));
},
_editMode: function() {
var _this = this;
var localModel = new Model({ id: this.model.get('type') + '_' + this.model.get('_id') });
localModel.fetch();
var model = undefined;
if (!localModel.get('_id')) {
console.log('not in localstorage');
model = new Model(getDoc(_this.model.get('_id')));
model.save();
} else {
console.log('was in localstorage');
model = localModel;
}
model.editView(this.model);
}
});
var ListView = Marionette.CollectionView.extend({
el: '#list',
collection: collection,
childView: ListItem,
initialize: function() {
this.render();
}
});
var myListView = new ListView();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment