Last active
August 29, 2015 14:07
-
-
Save pierr/4be260d5f5533c825b1a to your computer and use it in GitHub Desktop.
Backbone Promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Template d'une page de contact. | |
var templateContact = require('./template/contact'); | |
//Vue représentact la page de contact. | |
var ContactView = Backbone.View.extend({ | |
template: templateContact, | |
//Fonction d'initialisation de la vue de contact. | |
initialize: function initializeContactView(){ | |
this.listenTo(this.model, 'model:invalid', this.renderErrors, this); | |
this.listenTo(this.model, 'model:valid', this.save, this); | |
this.listenTo(this.model, 'change', this.render, this); | |
this.model.fetch(); | |
}, | |
//Evènements de la vue. | |
events: { | |
"click button['submit']": "validate" | |
}, | |
//parse the form and validate the model. | |
parseAndValidate: function(event){ | |
this.parse(); | |
this.model.validate(); | |
}, | |
//Save the model (on the server or on the localStorage); | |
save: function(){ | |
this.model.save(); | |
} | |
//Rend la vue de contact. | |
render: function renderContactView(){ | |
this.$el.html(this.template(this.model.toJSON())); | |
return this; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Template d'une page de contact. | |
var templateContact = require('./template/contact'); | |
//Vue représentact la page de contact. | |
var ContactView = Backbone.View.extend({ | |
template: templateContact, | |
//Fonction d'initialisation de la vue de contact. | |
initialize: function initializeContactView(){ | |
this.listenTo(this.model, 'change', this.render, this); | |
this.model.fetch(); | |
}, | |
//Evènements de la vue. | |
events: { | |
"click button['submit']": "save" | |
}, | |
//parse the form and validate the model. | |
save: function(event){ | |
var currentView = this; | |
this.parse() | |
.then(currentView.model.validate) | |
.then(currentView.model.save) | |
.catch(currentView.manageErrors); | |
}, | |
//Rend la vue de contact. | |
render: function renderContactView(){ | |
this.$el.html(this.template(this.model.toJSON())); | |
return this; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment