-
-
Save kaareal/2428305 to your computer and use it in GitHub Desktop.
Solution Backbone Model
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
Solution = Backbone.Model.extend ({ | |
fetchAll: function(options) { | |
var solution = this; | |
var error = false; | |
var errorHandler = function() { | |
error = true; | |
} | |
$.when(solution.comments.fetch({ error: errorHandler }), | |
solution.likes.fetch({ error: errorHandler }), | |
solution.team.fetch({ error: errorHandler }), | |
solution.assets.fetch({ error: errorHandler })).done(function(a, b, c, d) { | |
if(options.success && !error) | |
options.success(); | |
else if(options.error) | |
options.error(); | |
}); | |
}, | |
initialize: function() { | |
this.comments = new Comments(); | |
this.comments.url = this.url() + '/solution_comments'; | |
this.likes = new Likes(); | |
this.likes.url = this.url() + '/solution_likes'; | |
this.team = new Team(); | |
this.team.url = this.url() + '/team_memberships'; | |
this.assets = new Assets(); | |
this.assets.url = this.url() + '/solution_assets'; | |
}, | |
like: function(callback) { | |
if(!this.likes) { | |
this.likes.fetch({ success: this.handleLikesSuccess.bind(this) }); | |
} else { | |
this.handleLikesSuccess(); | |
} | |
}, | |
handleLikesSuccess : function() { | |
var like = this.likes.get(this.get('like_id')); | |
var solution = this; | |
if(like) { | |
like.url = this.likes.url + '/' + like.get('id'); | |
like.destroy({ | |
success: function(model, response) { | |
this.like_id = null; | |
solution.likes.remove(model); | |
callback && callback(); | |
}.bind(this) }); | |
} else { | |
like = new Like(); | |
like.url = this.likes.url; | |
like.save(null, { | |
success: function(model, response) { | |
this.like_id = model.id; | |
solution.likes.add(model); | |
callback && callback(); | |
}.bind(this)}); | |
} | |
} | |
}); | |
Solutions = Pagable.extend({ | |
model: Solution | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment